Results 1 to 3 of 3

Thread: C code for reverse the order of words in a string

  1. #1
    HallMiller is offline Senior Member
    Join Date
    Dec 2009
    Posts
    251
    Rep Power
    3

    Default C code for reverse the order of words in a string

    Hi. I am learning C programming language. Anybody knows how to reverse the order of words in a string. If you have any in code please let me know that. Thanks in advanced.

  2. #2
    HallMiller is offline Senior Member
    Join Date
    Dec 2009
    Posts
    251
    Rep Power
    3

    Default

    Refer following program to reverse the order of words in a string:

    Code:
    #include <stdio.h>
    #include <string.h>
    char string[20];
    void strrev(char *start, char* end)
    {
     char temp;
     
     if( !start || !end ) //null pointer check
      return;
     
     for(; start<=end; start++, end-- )
     {
      temp = *start;
      *start = *end;
      *end = temp;
     }
    }
     
    int main()
    {
     char* tempstr = string;
     char* tempstr2;
     printf("Enter the string with words: ");
     scanf("%[^n]s", string);
     strrev( tempstr, tempstr+strlen(tempstr)-1 );
     printf( "%sn", tempstr, string );
     while( *tempstr!=0 )
     {
      tempstr2 = tempstr;
      while( tempstr2 && (*tempstr2!=' ' && *tempstr2!=0) )
        tempstr2++;
      strrev( tempstr, tempstr2-1 );
      tempstr = tempstr2+1;
     }
     printf( "%sn", string );
    }
    I have tried the given code but it didn’t solve my problem. Please try to provide some other solution.

  3. #3
    AllenBrown is offline Senior Member
    Join Date
    Dec 2009
    Posts
    240
    Rep Power
    3

    Default

    Try this code:

    Code:
    string s = "box is red"; //Input string
    string t = ""; //Output string
    string w = "";
    for (int i = 0; i < s.Length; i++)
    { 
    if (s[i] == ' ')
    {
    t += w + ' ';
    w = "";
    }
    else w += s[i];
    }
    t+= w; //Get the last word of string if there is not a ' ' at the end of input string

Similar Threads

  1. Program to reverse the order of words in a string
    By EvansMitchell in forum Programming
    Replies: 2
    Last Post: 07-07-2010, 02:05 PM
  2. I am going reverse to XP from Vista?
    By AndersonDiaz in forum Windows XP
    Replies: 2
    Last Post: 05-24-2010, 01:31 PM
  3. How to reverse the order
    By PetersonClark in forum Programming
    Replies: 1
    Last Post: 02-16-2010, 05:57 PM
  4. C program to reverse a number
    By Millerjames in forum Programming
    Replies: 1
    Last Post: 01-13-2010, 05:42 PM
  5. Configuring the reverse proxy (Layer 1)
    By carlos in forum Everything Else
    Replies: 0
    Last Post: 04-08-2009, 07:00 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
SEO by SubmitEdge

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48