i have written code reverse string. think logic correct. can compile it, unable run it. trying use mingw on windows. can point out problem might be?
void reverse(char * start, char * end){ char ch; while(start != end){ ch = *start; *start++ = *end; *end-- = ch; } } int main(){ char *c = (char *)"career"; int length = strlen(c); reverse(c,c+length-1); } thanks
you passing literal function , attempting modify literals undefined behaviour.
make modifiable string this:
char c[] = "career"; on top of that, reverse works when have odd number of characters in string. while condition wrong. should be:
while(start < end) your code said:
while(start != end) and if string has number of characters, condition true. hence loops until segmentation fault because start , end point outside input string.
Comments
Post a Comment