Q:

C program to reverse a string without using library function

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C program to split string by space into words... >>
<< C program to concatenate two strings without using...