On line 18, I get a seg fault in the first iteration (i = 0).
#include <stdio.h>
int main(void) {
char* str = "mono";
int length = 0;
int i;
for (i = 0; ; i++) {
if (str[i] == '\0') {
break;
} else {
length++;
}
}
for (i = 0; i < length / 2; i++) {
char temp = str[length - i - 1];
str[length - i - 1] = str[i]; // line 18
str[i] = temp;
}
printf("%s", str);
return 0;
}
I wrote this algorithm to reverse a string.