Can someone tell me how to delete a character from a string? Thanks.
Printable View
Can someone tell me how to delete a character from a string? Thanks.
Copy all the characters which follow it down one place in the array
There are a couple ways...
The first that comes to mind involves running through the source string character by character and copying each one to a temp string, except the character you are trying to delete, and then assign the original variable to the address of the temp string.
To omit the character you are trying to delete, you could use an if that checks whether the current character you are about to copy is the index of the character you are trying to delete, and if it is not then continue with copying that character.
This is by no means the fastest way, but if you go through the steps, it should be fairly easy to convert each step to the appropriate c code to get the job done.
You might also google around for the c string functions like strncpy(). Use some imagination and you can easily come up with something that uses strncpy() and is faster than the method I outlined above.
You can also do what Salem suggested... jeez I'm slow when I post. :)
Code:#include <stdio.h>
int main(){
char str[10] = { 'A','B','C','D','E','F','G','H','I', 0 };
char chr_to_find = 'C';
int i = 0, j = 0;
printf( "%s\n",str );
while( str[i] ){
if( str[i++] != chr_to_find )
++j;
str[j] = str[i];
}
printf( "%s\n", str );
return 0;
}
I just thought of this, so I'm not sure if it will work but
Looks like it might not be efficient but it could be fun to bug a prof this way :pCode:char *str, *tmp, *ptr;
strcpy(str,"a string example");
//lets remove the x in 'example' from this string
//x is located at index 10
str[10]='\0'; //now the computer thinks that this is the end of the string
strcpy(tmp,str);//tmp now has "a string e"
ptr=str[11]; //ptr now is "ample"
strcat(tmp,ptr); //tmp now has the final string of "a string eample"
try this
regardsCode:
while(s[i]!='\0')
{
if(s[i]=='C') continue;
s[j++]=s[i++];
}
s[j]='\0'
harish
Why do you people use loops? To illustrate what Salem said:
Because strlen(s+i) is one more than strlen(s+i+1), copying strlen(s+i) characters starting from s+i+1 includes the '\0'.Code:// Given: s is string, i is index which is to be removed:
memmove(s+i, s+i+1, strlen(s+i));
Ideally, you'd already have the length of s somewhere.
You should know that this code will just cause an infinite loop and will never do what you want it to. Perhaps you should "try this" before you suggest some one else do it.Quote:
Originally Posted by ssharish
Quzah.
very sorry about the code which i posted. it seems to be an infinite looop. quzah thax for finding that it is an infinte loop. but i can't still understand why that loop don't terminate. can any one tell me why is it so..
Because you don't increment i when the current character is 'C'. You just continue without incrementing. So it hits that letter and the next time it loops it's still on the same letter, and the next time it's still on the same letter, etc. etc. until your CPU throws up and dies of the tedium.
thanks very much itsme86
the code is working fine for me now