Q. Write a C program to read a string from user and display string in reverse order.
Example:
User entered string: This is a good blog
Result/output: blog good a is This
Ans.
/*c program for reverse all string*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char str[30];
int i,tmp;
printf("Enter any string: ");
gets(str);
for(i=0; str[i]!='\0'; i++);
for(i--; i>=0;i--)
{
if(str[i-1]==' ' || i==0)
{
for(tmp=i; str[tmp]!='\0' && str[tmp]!=' '; tmp++)
printf("%c",str[tmp]);
}
printf(" ");
}
getch();
return 0;
}
/***************Output*****************/
Related Programs:
Example:
User entered string: This is a good blog
Result/output: blog good a is This
Ans.
/*c program for reverse all string*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char str[30];
int i,tmp;
printf("Enter any string: ");
gets(str);
for(i=0; str[i]!='\0'; i++);
for(i--; i>=0;i--)
{
if(str[i-1]==' ' || i==0)
{
for(tmp=i; str[tmp]!='\0' && str[tmp]!=' '; tmp++)
printf("%c",str[tmp]);
}
printf(" ");
}
getch();
return 0;
}
/***************Output*****************/
Screen shot for reverse all string C program |
Related Programs: