6.01.2012

Reverse all words but not string

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********************/
Output of reverse all words but not string C program
Screen shot for reverse words in string c program


Related Programs:

  1. Reverse all string C program
  2. Reverse each first character of word & add extra character
  3. Change case of string(toggle/Title Case) C program
  4. Search sub string from main string C program
  5. Search sub string individual from main string
  6. Display string vertically C program
  7. Position and repetition of character from string C program

12 comments:

  1. Nice and sweet program.....

    ReplyDelete
  2. it will be more accurate if we change a little:

    #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;
    }

    ReplyDelete
  3. it s not reversing by word...exact word by reversing is eg:
    IP :
    hai good morning
    OP:
    morning good hai

    ReplyDelete
  4. IP:hai good morning
    OP:morning goof hai
    this is exact reversing by word.....

    ReplyDelete
    Replies
    1. #include
      void 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);
      }

      Delete
  5. write a program if we enter pasward in show ***. on display..

    eg.. if user inter at run time 123 then it should not display on scrren or display ****

    ReplyDelete
  6. This is my version

    void _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);
    }

    ReplyDelete
  7. nice program and nice comment programs

    ReplyDelete
  8. add before 17 th line==> if(s1[i+1]==' ')

    so we can reduce the space consumption in this original code..

    ReplyDelete
  9. #include
    #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;

    }

    ReplyDelete