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.
/*c program for reverse all words in string*/
#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********************/
Related Programs:
Let's assume string is: This Is A Good Blog
We wants to do: sihT sI A dooG golB
Ans.
/*c program for reverse all words in string*/
#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********************/
Screen shot for reverse words in string c program |
Related Programs:
- Reverse all string C program
- Reverse each first character of word & add extra character
- Change case of string(toggle/Title Case) C program
- Search sub string from main string C program
- Search sub string individual from main string
- Display string vertically C program
- Position and repetition of character from string C program
Nice and sweet program.....
ReplyDeletevery nice logic
ReplyDeleteNICE ONE..
ReplyDeleteit will be more accurate if we change a little:
ReplyDelete#include
#include
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)
{
printf(" ");
for(temp=i; temp>=0 && str[temp]!=' '; temp--)
printf("%c", str[temp]);
}
}
getch();
return 0;
}
it s not reversing by word...exact word by reversing is eg:
ReplyDeleteIP :
hai good morning
OP:
morning good hai
IP:hai good morning
ReplyDeleteOP:morning goof hai
this is exact reversing by word.....
#include
Deletevoid main()
{
int i,j,sp=0,index,temp;
char str[20];
gets(str);
for(i=0;str[i]!='\0';i++)
if(str[i]==' ')
sp++;
index=i;
while(sp--)
{
index--;
while(str[index]!=' ')
index--;
for(j=index+1;j!=i;j++)
printf("%c",str[j]);
printf(" ");
i=index;
}
for(j=0;j<i;j++)
printf("%c",str[j]);
printf("%c",10);
}
write a program if we enter pasward in show ***. on display..
ReplyDeleteeg.. if user inter at run time 123 then it should not display on scrren or display ****
This is my version
ReplyDeletevoid _ReverseWordOrder(char *const lacString){
char *ptr_lacString;
char *ptr_lacWord;
char lacAux[kMAX_STRING]={NULL};
ptr_lacWord=strrchr(lacString,kcSPACE);
while(NULL!=ptr_lacWord){
strcat(lacAux,ptr_lacWord+1);
strcat(lacAux,ksSPACE);
*ptr_lacWord='\0';
ptr_lacWord=strrchr(lacString,kcSPACE);
}
strcat(lacAux,lacString);
strcpy(lacString,lacAux);
}
nice program and nice comment programs
ReplyDeleteadd before 17 th line==> if(s1[i+1]==' ')
ReplyDeleteso we can reduce the space consumption in this original code..
#include
ReplyDelete#include
#include
int main()
{
char s1[100],s2[100];
int i,j,k,m=0;
printf("Enter the string\n");
gets(s1);
j=0;
for(i=0;s1[i]!='\0';i++)
{
if(s1[i+1]==' ' || s1[i+1]=='\0')
{
for(k=i;k>=0&&s1[k]!=' ';k--)
//printf("%c",s1[k]);
s2[j++]=s1[k];
s2[j++]=' ';
}
//printf(" ");
}
s2[j]='\0';
printf("%s\n",s2);
return 0;
}