反转字符串中的每个特定行

我试图使用递归方法在文本文件中反转我的行。 我现在非常困难,我目前的输出是分段错误 – 有人可以解释分段错误是什么来推动我朝着正确的方向前进吗?

void RecursionLine(); int main (int argc, char argv) { RecursionLine(); printf("\n"); } void RecursionLine() { int c; if((c = getchar()) != EOF || (c != '\n')) { RecursionLine(); printf("%c",c); } else if((c = getchar()) != EOF && (c == '\n')){ printf("\n"); RecursionLine(); } } Input: Dogs Cats Boys Output sgoD staC syoB 

您收到分段错误,因为您有|| 在你的第一个if语句中的条件,其中一个条件总是为真,导致你的堆栈从无限递归溢出! 将此更改为&& ,它应该全部修复!

 if((c = getchar()) != EOF && (c != '\n')) 

编辑:另外我相信你会因为第二个getchar()而遇到一些不正确的function。 我会将你的function改为:

 void RecursionLine() { int c = getchar(); if(c != EOF || c != '\n') { RecursionLine(); printf("%c",c); } else if(c != EOF && c == '\n'){ printf("\n"); RecursionLine(); } } 

否则,您将在每次迭代时读取可能的2个字符,这将导致其中一个/两个被跳过!