C Program for SWAPPING THE STRING
#include conio.h>
#include stdio.h>
void main()
{
char a[50],temp=0;
int i=0,c=0,c1=0,l=0;
clrscr();
printf("ENTER THE STRING= ");
flushall();
do
{
a[i++]=getchar();
}while(a[i-1]!='\n');
a[i]='\0';
for(i=0;a[i]!='\0';i++)
{
if(a[i]==' ' || a[i]=='\n')
{
c++;
}
}
printf("\nTHE TOTAL NO OF WORDS BEFORE SWAPING IS %d\n\n",c);
l=strlen(a);
l--;
for(i=0;a[i]!='\0';i++)
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
i++;
}
for(i=0;a[i]!=0;i++);
if(l%2!=0)
{
temp=a[i-2];
a[i-2]=a[i-1];
a[i-1]=temp;
}
else
{
a[i]='\n';
a[i+1]='\0';
}
printf("%s",a);
for(i=0;a[i]!='\0';i++)
{
if(a[i]==' ' || a[i]=='\n')
{
c1++;
}
}
printf("\nTHE TOTAL NO OF WORDS AFTER SWAPING IS %d\n\n",c1);
getch();
}
OUTPUT :
ENTER THE STRING: KAMLESH D MENGAR
THE TOTAL NO OF BEFORE SWAPPING 3
AKLMSE H DEMGNRA
THE TOTAL NO OF AFTER SWAPPING 3