Hi
can anyone teach me how to remove spaces + punctuation in a String?
eg.
String word = "Help me! Help me!";
after removespaces+punctuation
print:
HelpmeHelpme
Thank you very much!
Hi
can anyone teach me how to remove spaces + punctuation in a String?
eg.
String word = "Help me! Help me!";
after removespaces+punctuation
print:
HelpmeHelpme
Thank you very much!
check each character in the string... if it's one you want to delete, then delete it and shift all the other remaining characters back one...
hasafraggin shizigishin oppashigger...
Somthing like thatCode:for (int x=0; x<strlen(word);x++) { if(word[x] = ' ') { delete word[x]; } else if(word[x] = '!') { delete word[x]; } }
look into standard function strtok()...do a search on this board....
Xterria. You do not use delete like that mate. That is a recipe for disaster.To do this look into the functions isalpha,isspace and ispunct. This will allow you to identify which chars should stay and which should go. Do not do what xterria tried to show you.
Free the weed!! Class B to class C is not good enough!!
And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi
I am having the same issue. I need to find a palindrome, but I can't get my stuff to shift over after I call the space's and puntuation Null.
Basically, I sitll need to find a way to reverse the string and do a strcmp(original strings with all lower-space and then removed spaces and punctuation, new reversed string with all punctuation/spaces removed, and lower spaced).
I really need help on the part where I do remove all the spaces/punctuation.
Right now in function removeSpaces, I look to see if it is basically not a lower case letter or a number, and if it isn't it does _____. I need the _____ to remove and shift the string the the left... any help would be GREATLY appreciated.
And any tips on the string reversal. I heard about strrev.... does that work in C programming?
Thanks a ton!
Also I don't know how to post the code straight to here so.... Im sorry!
Last edited by Computergeek931; 11-17-2010 at 07:44 PM.
Okay I figured out how to post code. But yea, heres what it is.
Takes in "input.txt" as a string
Code:#include<stdio.h> #include<stdlib.h> #include<string.h> #define N 45 #define LENGTH 80 int readline(FILE* f, char *line); void swap(char *string1, char *string2); void sort(char name[N][LENGTH]); void lower(char name[N][LENGTH]); void removeSpace(char name[N][LENGTH]); int main(void) { FILE *inp; int i,j; char name[N][LENGTH]; int countLine = 0; int len = 0; inp = fopen("input.txt","r"); for(i = 0; i < N; i++){ len = readline (inp, name[i]); if( len == 0 ) break; } countLine = i; lower(name); sort(name); removeSpace(name); for(i = 0; i < N-1; i++){ printf("%s\n",name[i]); } } void lower(char name[N][LENGTH]) { int i,j; for(i = 0; i < N-1; i++){ for(j = 0; j < strlen(name[i]); j++){ name[i][j]=tolower(name[i][j]); } } } void removeSpace(char name[N][LENGTH]) { int i,j; for(i = 0; i < N-1; i++){ for(j = 0; j < strlen(name[i]); j++){ if((name[i][j] < '0') || (name[i][j] < 'a' && name[i][j] > '9') || name[i][j] > 'z') { name[i][j] = '\0'; strcat(name[i],&name[i][j]); } } } } void sort(char name[N][LENGTH]) { int i; int j; for(i = 0; i < N-1; i++){ for(j = i+1; j < N; j++){ if(strcmp(name[i], name[j])>0) swap(name[i], name[j]); } } } int readline(FILE* f, char *line) { int len = 0; char c; while ( (c = fgetc(f) ) != EOF && c != '\n') { line[len++] = c; line[len] = '\0'; } return len; } void swap(char *string1, char *string2) { char t[LENGTH]; strcpy(t, string1); strcpy(string1, string2); strcpy(string2, t); }
You could simplify it to a function that replaces all punctuation with a space, and a function that removes spaces.
Well either way, I need to figure out how to remove the spaces...
any help on that?
This looks like it would be better off in the C forum though from your initial post it looks like you were trying to use a std::string container for your "word"s, so... are you looking for C or C++ assistance?
#1.
The fgetc function returns an int, not char.Code:int readline(FILE* f, char *line) { int len = 0; char c; while ( (c = fgetc(f) ) != EOF && c != '\n')
#2.
I believe you've got some "off by one" errors there in red.Code:int main(void) { ... for(i = 0; i < N; i++){ len = readline (inp, name[i]); if( len == 0 ) break; } ... for(i = 0; i < N-1; i++){ printf("%s\n",name[i]); } } void lower(char name[N][LENGTH]) { int i,j; for(i = 0; i < N-1; i++){ ... } void removeSpace(char name[N][LENGTH]) { int i,j; for(i = 0; i < N-1; i++){ ... }
#3. It's possible to not read the full N strings from a file but all your code would assume N string were read and you would therefore attempt to print/sort/etc N strings that potentially contained garbage.
If you were looking at a C++ solution using a std::string container instead I'd suggest looking at the isspace and ispunct functions along with the STL std::remove_if function. Then the removal of both punctuation and spaces could each be a one-line thing.
Reversal of the string is also a one-liner using reverse iterators to initialize a new std::string object using the original. ...and lowering the case is another one-liner using the STL std::transform function along with tolower.
Last edited by hk_mp5kpdw; 11-18-2010 at 06:59 AM.
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
Thanks. The functions work just fine though.
I just need to figure out how to take the whole string and shift it left.
right now it out puts
'to' instead of 'to take out the garbage' becasue when it reads a space, the /0 character indicates the end of the strgin.
isnt there like...a delete key for C?lol