Reads in a line of input and then prints the line in reverse order. - C String

C examples for String:String Console Input

Description

Reads in a line of input and then prints the line in reverse order.

Demo Code

#include <stdio.h>

int main(void){
  char line[255];
  int i = 0; // array index

  printf("Enter a line to reverse:\n");
  while (scanf("%c", &line[i]), line[i] != '\n')
    i++;//w  w w  . jav  a 2s  . c o  m

  for (; 0 <= i; i--) // previous loop leaves i in right position
    printf("%c", line[i]);

  printf("\n");

  return 0;
}

Result


Related Tutorials