The code that "reverses" the line as soon as the user presses ENTER:

#include <stdio.h> #define MAXLINE 40000 char reverse(char string[]); int main() { int c; char string[MAXLINE]; for (int i = 0; i <= MAXLINE; ++i) string[i] = 0; int i = 0; while ((c = getchar()) != EOF){ string[i] = c; ++i; if (c == '\n'){ for (int e = i; e >= 0; --e) putchar(string[e]); char string[MAXLINE]; for (int i = 0; i <= MAXLINE + 1; ++i) string[i] = 0; i = 0; putchar('\n'); } } } 

Here is what it does:

 timur@timur:~/Documents/C/Part_1$ ./ex_1_19 hello world #ввод данных + ENTER #почему пустая строка? dlrow olleh #вывод данных (правильно) #просит еще раз что-то ввести. Я нажимаю ENTER e #почему она выводит??? #странная пустая строка #ввожу enter e #почему она выводит??? #странная пустая строка #ввожу enter e #почему она выводит??? #странная пустая строка hello world #ввод данных #странная пустая строка dlrow olleh #вывод good bye #ввод l #странная строка eyb doog #вывод f*cking C #ввод d #странная строка C gnikc*f #вывод 

How to implement the reverse array function?

    1 answer 1

    You probably wanted this one?

     int main() { int c; char string[MAXLINE] = ""; int i = 0; while ((c = getchar()) != EOF){ string[i] = c; ++i; if (c == '\n'){ for (int e = i-2; e >= 0; --e) putchar(string[e]); i = 0; putchar('\n'); } } } 

    Look at the difference - and everything will become clear ...

    • why i-2 and not i-1? - Aminev Timur 4:34 pm
    • Not to print \n - why? - Harry
    • get text \ n \ 0? - Aminev Timur
    • You print character by character - you don’t need a \0 at all . Here you entered, say, hello\n - after entering you i == 6 . And you need to olleh - i.e. characters number 4,3,2,1,0. - Harry