Search This Blog

Translate

C program to Reverse Each Word of Given String


MCA Management Solved Question


WAP C program  to Reverse Each Word of Given String


#include
#include
void revwords(char string[50]);
//function declaration
void main()
{
char string[50],choice;
clrscr();

printf("Enter String :");
gets(string);
revwords( string);
//calling Function
getch();
}
void revwords(char string[50])
//function Defination
{
int i=0,j=0,k=0;
char temp[50];

while (string[i]!='\0')
{
if (string[i]==' ')
{
j=i;
while(k<=i)
{
temp[k]=string[j--];
printf("%c",temp[k]);
k++;
}
}
i++;
}
if (string[i]=='\0')
{
j=i;
while(k<=i){
temp[k]=string[j--];
printf("%c",temp[k]);
k++;
}
}

_____________________________________ 


WAP to Reverse Each Word of Given String

#include
#include
void main()
{
char string[50],choice;
int i=0,j=0,k=0;
char temp[50];
clrscr();

printf("Enter String :");
gets(string);
while (string[i]!='\0')
{
//here we find spaces from string and store index of space in j
if (string[i]==' ')
{
j=i;
while(k<=i)
{
temp[k]=string[j--];
printf("%c",temp[k]);
k++;
}
}
i++;
}
//this is for word after space occor in string
if (string[i]=='\0')
{
j=i;
while(k<=i){
temp[k]=string[j--];
printf("%c",temp[k]);
k++;
}

}
getch();
}




 

C Program example List

Google+ Badge