avar avar - 2 months ago 9
C Question

how to replace a char in char *

total newbie here.
i was trying to replace a character in char * but my program gives error

#include <stdio.h>

int main(int argc, char **argv)
{
char *mystring ="love is alweys better yoe";
int count = 1;

for (count ; count < 23; count++)
{
if ((mystring[count] == 0x65 )) //&& ((mystring[count+1] > 0x41) && (mystring[count+1] < 0x7A)))
{
mystring[count] = 0x45; //here occur the freezing
printf ("%c\n", mystring[count]);
//break;
};
};

printf("%s\n",mystring);
return 0;
}

Answer

The

char *mystring ="love is alweys better yoe"

makes mystring read-only

you need to copy the string into a buffer before you can change it

e.g.

char mystring[128];
strcpy( mystring , "love is alweys better yoe" );