这是来自 K 和 R 的一个问题。当我尝试编译它时,它说分段错误(核心转储)。但是,我似乎找不到错误。
#include <stdio.h>
这是你的基本交换
void swap(char s[], int i, int j) {
char temp;
temp = s[i];
s[i] = s[j];
s[j] = temp;
}
void reverse(char s[]){
int i, j;
if (i == 0)
j = strlen(s)-1;
swap(s, i, j);
j--;
i++;
// Here is where the problem arises. When i don't call the function here the program works perfectly (The limitation being only the first and last char get swapped) otherwise it gives an error saying segmentation fault
if (i < j)
reverse(s);
}
int main () {
int i;
char s[10] = "hello";
reverse(s);
printf("%s", s);
}