if you know where in the string the character is, then its as simple as just writing over it. If you dont know where it is, then you have to do a loop and search through the string to find it, then write over it.
example 1: The string char mystring[] = "Hello World." you want to replace the . with !. your code would be:
mystring[11] = '!';
the . is the 11'th element in the string.
example 2: you want to replace . with !, but dont know where it is in the string. the code would be:
for (int count = 0; count %26lt; sizeof(mystring); count++)
{
if (mystring[count] == '.')
{
mystring[count] = '!';
break;
}
}
How can I replace a character in a string in C?
delete tat character and add another
Reply:You have to loop through each character in that string and replace it with a character of your desire.
Ex:
for(x = 0; x %26lt; strlen(text); x++)
{
text[x] = 'X';
}
For more free C/C++ source codes, visit: http://smartcoder.co.nr
KaBalweg
http://smartcoder.co.nr
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment