ADVERTISEMENT
ADVERTISEMENT

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 »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.