Home »
C programs »
C Strings User-defined Functions Programs
C program to reverse a string without using library function
In this program, we will learn how to reverse a string without using library function?
Here, we are declaring two strings (character arrays), first string will store the input string and other will store the reversed string.
#include <stdio.h>
#include <string.h>
int main()
{
char str[100],revStr[100];
int i,j;
printf("Enter a string: ");
scanf("%[^\n]s",str);//read string with spaces
/*copy characters from last index of str and
store it from starting in revStr*/
j=0;
for(i=(strlen(str)-1); i>=0;i--)
revStr[j++]=str[i];
//assign NULL in the revStr
revStr[j]='\0';
printf("\nOriginal String is: %s",str);
printf("\nReversed String is: %s",revStr);
return 0;
}
Output
Enter a string: This is a test string
Original String is: This is a test string
Reversed String is: gnirts tset a si sihT
C Strings User-defined Functions Programs »