Friday, 25 January 2013

A program to reverse the words of a sentence

eg. i am a good boy
result: boy good a am i.


#include<stdio.h>
#include<conio.h>
void main()
{
char a[20],c[20];
char x=' ';
int k,l,m,i,j,r,b[10];
j=0;
for(i=0;i<20;i++)
c[i]=' ';
clrscr();

printf("enter a sentence:");
for(i=0;i<20;i++)
scanf("%c",&a[i]);
for(i=0;i<20;i++)
printf("%c",a[i]);
for(i=0;i<20;i++)
{
if(a[i]==x)
{
b[j]=i;
j++;

}
}
//printf("%d",j);
r=0;
/*for(i=0;i<j;i++)
printf("%d",b[i]+1);*/
printf("\n");
for(m=1;m<=j;m++)
{
l=b[j-m];
for(i=r;i<20;i++)
{
if(a[l+1]!='\0'&& a[l+1]!=' ' &&l<20)
{
c[i]=a[l+1];
l++;
r++;
}
}
r++;
c[r]=' ';
}


for(i=0;i<20;i++)
{
printf("%c",c[i]);
}
getch();
}

NOTE: give one space before entering the input and many spaces after completion of sentence so that input exceeds the no. of elements in array.






No comments:

Post a Comment