2

I've been stuck on this for a while and any help would be greatly appreciated.

I'm trying to reverse input one line at a time but display it all together at the end.

Eg Input = abc 123
           lorem ipsum
           dolor sit amet


Output = 321 cba
         muspi merol
         tema tis rolod

This is what I have so far, but it only reverses the last line of input. My thoughts are that I am possibly not reading in all the input, but am unusure how to remedy this?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void reverse(char *input, int length){
    for(int i = length-1; i >= 0; i--){
        printf("%c", input[i]);

    }

}

int main(void){
    char input[100];

    while(!feof(stdin)){

        fgets(input, 100, stdin);

    }

    reverse(input, strlen(input));


    return 0;
}
jordan11
  • 29
  • 2
  • You can write a function that takes pointers to the beginning and the end of a string and swaps the string and then call that on every line (omitting the newline, of course). – S.S. Anne Feb 19 '20 at 00:29
  • 1
    Anything you put inside your `while` loop will be executed for each line. Anything you put outside your while loop will only be executed once. – that other guy Feb 19 '20 at 00:31
  • @jordan11 Do you need to reverse inputted string and output them already reversed or just to output them in the reverse order? – Vlad from Moscow Feb 19 '20 at 00:34
  • @thatotherguy when I put it outside the while loop, it does reverse each line, but doesn't print it all together at the end. It prints as soon as I hit enter. Any ways around this? – jordan11 Feb 19 '20 at 00:35
  • @VladfromMoscow reverse each line of the input string and then output it together, but keep the order that they were inputted in – jordan11 Feb 19 '20 at 00:39
  • @jordan11 There's already a standard tool `rev` to reverse lines, and it behaves like that as well. Similarly, other standard tools like cat/grep/sed/awk/tee process lines and show relevant output immediately. Are you sure you want to do it in this different, non-canonical way, and that you're not e.g. inferring something from an assignment that isn't there? – that other guy Feb 19 '20 at 00:44
  • 1
    `while(!feof(stdin)){` --> [Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/q/5431941/2410359). – chux - Reinstate Monica Feb 19 '20 at 05:17
  • Welcome to SO, and congratulations: You actually provided input data, expected output, actual output, and a complete program which compiles (I think). I think this is the first time *ever* I have seen this from a newcomer. I'm looking forward to your questions in a year or two. – Peter - Reinstate Monica Feb 19 '20 at 11:34

2 Answers2

2

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.

srilakshmikanthanp
  • 2,231
  • 1
  • 8
  • 25
1

As noted in the comments,

Anything you put inside your while loop will be executed for each line. Anything you put outside your while loop will only be executed once.

So put your reversing code in the loop, together with fgets.

anatolyg
  • 26,506
  • 9
  • 60
  • 134