Program Of Reversing String In C
Reverse String :
String is the sequence of characters terminated with a null character ‘\0’. Reverse string is printing the character from end to start
How to reverse a String in C :
In general, String can be reversed without using in-built method is loop. By using loop we can reverse the string easily.
Below are the two techniques to reverse the string using for loop :
Method 1 :
#include <stdio.h> int main() { char s[9] = "atsniperP"; // declaration of string printf("The reverse string is : "); for(int i=8;i>=0; i--){ // loop to convert the string printf("%c", s[i]); } return 0; }
Output :
The reverse string is : Prepinsta
In the above program, we traverse the string using for loop from back and print the particular character of the string to get the reverse string.
Method 2:
#include <stdio.h> int main() {
char s[9] = "atsniperP"; // declaration of string printf("The reverse string is : "); for(int i=0;i<9; i++){ // loop to convert the string printf("%c", s[8-i]); } return 0; }
Output :
The reverse string is : Prepinsta
In the above program, we traverse the string using for loop from start and print the particular character of the string to get the reverse string by using [n-i] to get the particular character from back..
For more related content : Click-Here
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment