Saturday 26 October 2013

Reverse of a String

#include<stdio.h>
void main()
{
    char a[20];
    char b[20];
    int i=-1,j=0;// i =-1 because index starts from zero.
    printf("enter the string :- ");
   gets(a);  
    while(a[++i]!='\0'); // i denotes the last character of the string through this loop
    while(i>=0)
     b[j++] = a[--i];//copy last character of "a" char array string as the first character into the "b" char array. 
    b[j]='\0'; //insert null to terminate the string
    printf("Reverse  string is : %s",b);

}

No comments :

Post a Comment