- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Write a C program to print the message in reverse order using for loop in strings
Here we write a program to reverse the sentence without predefined functions. By using for loop, we can easily print statement in reverse order.
Program 1
#include<stdio.h> int main(){ char stmt[100]; int i; printf("enter the message:
"); for(i=0;i<stmt;i++){ stmt[i]=getchar(); //reading each char from console till enter or newline char is pressend if(stmt[i]=='
') break; } printf("the reverse statement is:
"); for(i--;i>=0;i--) //printing each char in reverse order putchar(stmt[i]); putchar('
'); return 0; }
Output
enter the message: Hi welcome to my world the reverse statement is: dlrow ym ot emoclew iH
Program 2
Here, we will write a C program to reverse a string using strrev library function −
#include<stdio.h> #include<string.h> void main(){ //Declaring two strings// char result[50],string[25]; //Reading string 1 and String 2// printf("Enter String to be reversed : "); gets(string); //Reversing using library function// strrev(string); printf("The reversed string is : "); puts(string); }
Output
Enter String to be reversed : Hi welcome to tutorials Point The reversed string is : tnioP slairotut ot emoclew iH
Advertisements