Loading

Archive for 26 Oct 2012

C - Program to Reverse Words in a Sentence

C - Program to Reverse Words in a Sentence


#include<stdio.h>
#include<conio.h>

void main()
{
  int i,j,k;
  char s[100],c;
  clrscr();

  printf("\n\nEnter a Sentence :: ");
  gets(s);

  i=0;
  while(s[i]!='\0')
  {
    j=k=i;
    if(s[i]==32)
    {
    i++;
    continue;
    }
    while(s[i]!=32)
    {
    i++;
    if(s[i]=='\0')
    break;
    }
    j=i-1;
    while(k<=j)
    {
    c=s[k];
    s[k]=s[j];
    s[j]=c;
    k++;
    j--;

    }
  }
  j=0;
  i--;
  while(j<=i)
  {
    c=s[j];
    s[j]=s[i];
    s[i]=c;
    j++;
    i--;
  }

  printf("\n\nReverse :: %s",s);
  getch();
}

Posted in | 1 Comment