replace char in string | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

replace char in string

i'm creating program to make letters lower_case, i have problem with replacing char in string saved in char* - result dumps core https://code.sololearn.com/coCBw45kC4IW/?ref=app problem -> s[i] += 'a' - 'A';

6th Nov 2019, 7:29 PM
nicolas turek
nicolas turek - avatar
7 Answers
+ 6
"HELLO WORLD" is stored in read only memory. char* is an incorrect type to use, it should be const char*. Attempting to write to read only memory will result in an access violation. If you used char x[] then the read only memory will be copied to the stack, which is writeable, for x to use. So no access violation happens there. If you really want to use a pointer you have to allocate memory from the heap yourself.
6th Nov 2019, 8:07 PM
Dennis
Dennis - avatar
+ 1
You could have used a character array to store a string and there is really no need of character pointers because you know things get ugly with pointers.
6th Nov 2019, 7:54 PM
Avinesh
Avinesh - avatar
+ 1
well this solution I've already find out, I'd like to do it with pointer
6th Nov 2019, 7:55 PM
nicolas turek
nicolas turek - avatar
0
thanks, this is right answer :D
6th Nov 2019, 8:08 PM
nicolas turek
nicolas turek - avatar
0
ok, now there is second problem - changing the string, any other advice?
6th Nov 2019, 8:22 PM
nicolas turek
nicolas turek - avatar
0
If you mean resizing, or adding/removing characters, this may help: https://stackoverflow.com/questions/2937409/resizing-an-array-with-c
6th Nov 2019, 8:44 PM
Dennis
Dennis - avatar
0
should i use some flush function?
6th Nov 2019, 8:49 PM
nicolas turek
nicolas turek - avatar