Try This,
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void reverse(char *str)
{
int i,j,length;
char ch;
for(length=0;str[length]!='\n'&&str[length]!='\0';length++); //for length of string litrel between '\n' or '\0' not for full length of the string
for(i=0,j=length-1;i<length/2;i++,j--) //to reverse string
{
ch=str[i];
str[i]=str[j];
str[j]=ch;
}
if(str[length]=='\n') //if the string literal is present
reverse(&str[length+1]); //then goes to recursion
else
return; //if no more string is present then return
}
int main(void)
{
char input[100];
int i;
memset(input,'\0',sizeof(input)); // initialize to null
for(i=0;!feof(stdin);i=strlen(input))
{
fgets(&input[i],(100-i), stdin); // &input[i] is to merge the inputting string to the before string present in the char input[100];
}
input[strlen(input)-1]='\0';
reverse(input);
printf("\n%s",input);
return 0;
}
Input:
abc 123
lorem ipsum
dolor sit amet
ctrl+z
Note: ctrl+z is to send EOF to stdin in windows, It must be in new line.
output in cmd:
abc 123
lorem ipsum
dolor sit amet
^Z
321 cba
muspi merol
tema tis rolod
Process returned 0 (0x0) execution time : 11.636 s
Press any key to continue.
Note :In this execution of code You Can't erase(backspace) after pressing Enter. if you want you should change the logic for input.
See Below:
The inputting string will stored will be like
"abc 123\nlorem ipsum\ndolor sit amet\0"
The reverse function reverses the string between the new line('\n') character only.if there is an null('\0') the function will return, else the function goes on recursion.
First pass:
"321 cba\nlorem ipsum\ndolor sit amet\0"
There is an '\n' after "321 cba" so the function passes reference of next character of the '\n' to the same function(recursion).
Second pass:
"321 cba\nmuspi merol\ndolor sit amet\0"
'\n' is present so goes to recursion.
Third pass:
"321 cba\nmuspi merol\ntema tis rolod\0"
'\n' is not present so function returns.