jordan11
jordan11

Reputation: 29

How to reverse multiple lines of input? (C)

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;
}

Upvotes: 2

Views: 1639

Answers (2)

srilakshmikanthanp
srilakshmikanthanp

Reputation: 2399

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.

Upvotes: 3

anatolyg
anatolyg

Reputation: 28279

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.

Upvotes: 1

Related Questions