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';
Printable View
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';
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.
Yep I just tried it and I didn't work :(
Thanks for the help :)
Well then obviously you didn't do it correctly. Post the code you used so we can show you where you went wrong.Quote:
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;
}
}
I meant the code I posted originally... I made the stupid mistake of not trying it before asking the question.
Quote:
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++;
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);
}
}
}
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++)
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?
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.