Say I have the string "hello", how would I remove the second 'l' entirely? Would this be right:
Code:char* str = "hello"; str[3] = '\0';
Say I have the string "hello", how would I remove the second 'l' entirely? Would this be right:
Code:char* str = "hello"; str[3] = '\0';
Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.
- Mike McShaffry
First of all, string literals aren't guaranteed to be writeable and typically aren't. That program would probably crash with a segmentation fault. Making str an array would fix it.
However, your method for removing a character would truncate the string entirely at that point so you'd lose the 'o' also. The only way to remove an item from an array is to move everything after that element up one spot in the array. memmove() is well-suited for the task or you can manually loop through.
If you understand what you're doing, you're not learning anything.
Yep I just tried it and I didn't work
Thanks for the help
Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.
- Mike McShaffry
Well then obviously you didn't do it correctly. Post the code you used so we can show you where you went wrong.Yep I just tried it and I didn't work
http://www.cplusplus.com/ref/cstring/memmove.html
edit: For a string this small, you might as well just go through manually. After you call memmove, you have to replace the o with something, otherwise you'll end up with "heloo". It' just as many lines of code to say
Code:str[3] = 'o'; str[4] = '\0';
I know, it sucks.. seems redundant and poor but it works.Code:int i = 0; BOOL is_second_l = FALSE; char string[] = "hello"; while ( string[i] != '\0' ) { if ( string[i++] == 'l' && !is_second_l ) is_second_l = TRUE; if ( is_second_l ) { strcpy(string + i, (string + i) + 1); break; } }
Last edited by xeddiex; 09-24-2005 at 11:27 AM.
I meant the code I posted originally... I made the stupid mistake of not trying it before asking the question.
Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.
- Mike McShaffry
Say I have the string "hello", how would I remove the second 'l' entirely?Code:char s[] = "hello", *p = s[3]; while(*p = *(p+1)) p++;
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Code:#include <iostream> void removeChar(char *TheStr, char ch) { int len = strlen(TheStr), c = 0; char Temp1[len], Temp2[len]; strcpy(Temp1,TheStr); for (int x = 0; x< len; x++) { if (Temp1[x] != ch) Temp2[c++] = Temp1[x]; } Temp2[c] = 0; strcpy(TheStr, Temp2); }
Your code is C99 code only. Meaning it won't compile on a good number of compilers out there.
Quzah.Code:void removeChar( char *s, char c ) { char *p = NULL; for( p = s; p && *p; p++ ) { if( *p == c ) { char *t = NULL; for( t = p; *t; t++ ) *t = *(t + 1); } } }
Hope is the first step on the road to disappointment.
it compiles fine under dev-cpp & visual studio
It's this line here
that's causing more of the importability. (And I don't know if you should use len as an array dimension.) It's also pretty ineficient - it uses two buffers, and Quzah's uses none.Code:for (int x = 0; x< len; x++)
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Lets not forget the quzah's uses pointers and pointers are your god!
im new to c/c++ programming and i havn't learned anything about pointers yet. im still trying to wrap my head around structures.
Just a thought... what about using memmove() to shift the last part of the string?
Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah
You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie
That would work too. Although you couldn't use memcpy(), because the buffers would probably overlap.
Because memove() allows overlapping buffers, it must copy all the data somewhere and then back again. This is inefficient. Quazh/dwks method with pointers is the best way to do it.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.