- Код: Выделить всё
#include <stdlib.h>
#include <stdio.h>
int main(int argc, const char * argv[])
{
const char *s = "This is a test";
int i = 0;
int new_len = 0;
// We don't know what the final size of the string will be, so
// let's make one that's generally large enough. Can do more
// elaborate things here if you want a really robust solution.
char new_string[256] = {0};
// For each character in the string:
for (i=0; s[i] != '\0'; ++i)
{
if (s[i] == 't' && s[i+1] == 'e')
{
// If we find "te", add "gho".
new_string[new_len++] = 'g';
new_string[new_len++] = 'h';
new_string[new_len++] = 'o';
// ... and skip one character to ignore both the 't' and the 'e'.
++i;
}
else
{
// Otherwise just add the same character.
new_string[new_len++] = s[i];
}
}
// Add the null terminator at the end of our new string.
new_string[new_len] = '\0';
// Output the new string.
printf("%s\n", new_string);
system("pause");
}