Can someone tell me how to delete a character from a string? Thanks.
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
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
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.
Last edited by jEssYcAt; 02-14-2005 at 04:50 PM. Reason: Salem beat me to it!
abachler: "A great programmer never stops optimizing a piece of code until it consists of nothing but preprocessor directives and comments "
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; }
Currently Reading:
Mathematic from the birth of numbers,
Effective TCP/IP programming,
Data Compression: The Complete Reference,
C Interfaces and Implementations: Techniques for Creating Reusable Software,
An Introduction to Genetic Algorithms for Scientists and Engineers.
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 wayCode: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.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
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.Originally Posted by ssharish
Quzah.
Hope is the first step on the road to disappointment.
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.
If you understand what you're doing, you're not learning anything.
thanks very much itsme86
the code is working fine for me now