Program for Reverse Characters String
Reverse Characters String
We will write a C program to print reverse characters string and the number will be entered by the user. This will help to understand the basic structure of programming. In this program , we will print the reverse characters string easily by using proper syntax and algorithms.
Note:
In the following program we will display the reverse of string using while() loop.
Syntax for while() Loop:-
while(condition) { // Statements // Increment / Decrement }
Problem 2
Write a program to the reverse of given string using while() loop.
- Firstly, we have to enter the number.
- Then print the reverse of string.
Code
Run
#include<stdio.h> #include<string.h> int main () { char str1[50], temp; int i = 0, j = 0; printf (" Enter a string to be reversed = "); scanf ("%s", str1); j = strlen (str1) - 1; while (i < j) { temp = str1[j]; str1[j] = str1[i]; str1[i] = temp; i++; j--; } printf (" The reversed of the string = %s", str1); return 0; }
Output
Enter a string to be reversed = PrepInsta The reversed of the string = atsnIperP
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