Wednesday 2 March 2016

Reverse all words but not string

Q. Write a C program to reverse all words but not string.

Let's assume string is: This Is A Good Blog
We wants to do: sihT sI A dooG golB


Ans:

#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
 char str[100];
 int i,temp;
 printf("Enter any string : ");
 gets(str);
 for(i=0; str[i]!=NULL; i++)
 {
  if(str[i+1]==' ' || str[i+1]==NULL)
  {
   for(temp=i; temp>=0 && str[temp]!=' '; temp--)
     printf("%c", str[temp]);
  }
  printf(" ");
 }
 getch();
 return 0;

}

Output



No comments:

Post a Comment

Ads Inside Post