C Program to Reverse a Line String

In this c programming example we will learn how to reverese a line string

Algorithm

  1. Take an array of char which we need to print the string words
  2. Calculate length of the char array
  3. take temp array with specified length
  4. Now loop the char array and take individual character and store into temp array
  5. Print the reverse char array

 

Simple C example to reverse a line string

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

int stringLength(char s[]) {
   int i=0;

   while(s[i]!='\0')
      i++;

   return i;    
}

void string_reverse(char st[]) {
   int i,j,len;
   char ch;

   j = len = stringLength(st) - 1;
   i = 0;

   while(i < j) {
      ch = st[j];
      st[j] = st[i];
      st[i] = ch;
      i++;
      j--;
   }
}

int main (void) {
   char line[] = "C program to reverse line string";
  
   int i, j, n;


   n = stringLength(line);
    char reverse[n] , temp[n];
    reverse[n]="";

   for(i = n-1; i >= 0; --i) {

      for(j = 0; i >= 0 && line[i] != ' '; --i,++j)
         temp[j] = line[i];

      temp[j] = '\0';

      string_reverse(temp);
      
      strcat(reverse,temp);
      strcat(reverse," ");
   }

   printf("Original - %s\n", line);
   printf("Reversed - %s\n",reverse);

   return 0;
}

 

Output

Input String - C program to reverse line string

Reversed String - string line reverse to program C 

 

 

Subscribe For Daily Updates