Q. Write a C program to accept a string from user and reverse all words but not string using pointer.
For example:
String: This Is A Good Blog
Result: sihT sI A dooG golB
Ans.
/*c program for reverse all words but not string using pointer*/
#include<stdio.h>
#include<conio.h>
int main()
{
char str[30];
char *p,*tmp;
printf("Enter any string: ");
gets(str);
for(p=str; *p!='\0'; p++)
{
if(*(p+1)==' ' || *(p+1)=='\0')
{
for(tmp=p; *tmp!=' ' && tmp>=str; tmp--)
printf("%c",*tmp);
}
printf(" ");
}
getch();
return 0;
}
/************Output************/
For example:
String: This Is A Good Blog
Result: sihT sI A dooG golB
Ans.
/*c program for reverse all words but not string using pointer*/
#include<stdio.h>
#include<conio.h>
int main()
{
char str[30];
char *p,*tmp;
printf("Enter any string: ");
gets(str);
for(p=str; *p!='\0'; p++)
{
if(*(p+1)==' ' || *(p+1)=='\0')
{
for(tmp=p; *tmp!=' ' && tmp>=str; tmp--)
printf("%c",*tmp);
}
printf(" ");
}
getch();
return 0;
}
/************Output************/
Screen shot for reverse all words in string but string in same order C program |
No comments:
Post a Comment