Program to reverse a string in C Language

The program below shows how you can reverse a string in c program. In this program I have used two arrays,one array is for the actual string to reverse and another array will keep the reversed string.

The while function read the string from last to first and put each character in rev array.

Code:
#include<stdio.h>
#include<string.h>
void main(){
int i,j;
char str[20],rev[20];
printf("Program to reverse a given string*******\nEnter a String: ");
scanf("%s",str);
i=strlen(str);
j=0;
while(i>=0){
rev[j]=str[i-1];
i--;
j++;
}
printf("%s\n",rev);
getch();
}

OUTPUT:

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.