Thread: to Reverse the words in the string... help.. I just can't see the error...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    10

    Unhappy to Reverse the words in the string... help.. I just can't see the error...

    I want to make a string to reverse the words but remain the words position the same.. (if input is "Hello world haha", the output should be "olleH dlrow ahah"), what should I do.. can I use the recursive method.. but how.. here is what I have done...

    Code:
    #include <stdio.h>
    void revstr (char str[]);
    
    main()
    {
        char str[100];
        printf ("Enter the String :");
        gets (str);
        revstr (str);
        return 0;
    }
    
    void revstr (char str[])
    {
        int i=0, j, k;
        char word[50], reverse[50];
    
             for (i=0; str[i]!='\0'; i++)
    	{
    	    while (str[i]!=' ')
    	    {
    	        word[i]= str[i];
    	        word[i+1]='\0';
    	    }
             for (j=i, k=0; (j>=0) || (str[j]=' '); j--, k++)
    	{
    	    reverse[k] = word[j];
    	    word[j] = '\0';
    	    reverse[k+1] = ' ';
                        reverse[k+2] = '\0';
    	}
              puts (reverse);
    	}
    }
    What I did is to iniciate word array to store words reverse array to reverse the letters, and put out the reverse array string by string.. it seem to be the stupidest way to do so...
    Last edited by Isotopes; 03-18-2003 at 11:05 AM.
    It's the flaws that makes the perfect more perfect....

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Please use code tags when posting code. You can edit your original post using the "edit" button to add them.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    The process can be as simple or complex as you'd like. For instance, a simpler but more difficult to understand method would be to use two pointers and walk down the string. When the end pointer finds a space or the end of the string, reverse from the start pointer to the end pointer and assign the end pointer to the start pointer. Lather, rinse, repeat.
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    static void revstr ( char *start, char *end )
    {
      char hold;
    
      while ( start < end ) {
        hold = *start;
        *start = *end;
        *end = hold;
    
        start++;
        end--;
      }
    }
    
    int main ( void )
    {
      char buf[] =  "Hello world haha";
      char *last = buf + sizeof buf;
      char *start = buf;
      char *end;
    
      for ( end = start; end != last; start = ++end ) {
        while ( *end != '\0' && !isspace ( *end ) )
          end++;
    
        revstr ( start, end - 1 );
      }
    
      printf ( "%s\n", buf );
    
      return 0;
    }
    -Prelude
    Last edited by Prelude; 03-18-2003 at 08:24 PM.
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    10

    Question Question... Help... and Desperation.....

    Thanks for Prelude's reply really appricate a lot.. however, when I try to impliment into my programe with the functions, it have general protection exception error. and the programme halt. I don't understand what's went wrong. Could it be the problem of the string being pass down from the main program ?

    Here is the whole programe.
    Code:
    #include <stdio.h>
    #include <ctype.h>
    #incldue <string.h>
    
    int revstr (char str[]);
    static void reverse ( char *start, char *end );
    
    main()
    {
        char str[100];
        printf ("Enter the String :"); 
        gets (str);
        revstr (str);
        return 0;
    }
    
    int revstr (char str[])
    {
        char *last = str + sizeof str;
        char *start = str;
        char *end;
    
            for ( end = start; end != last; start = ++end ){
    	while ( *end != '\0' && !isspace ( *end ) )
    	end++;
    
            reverse ( start, end - 1 );
      }
    
      printf ( "%s\n", str );
    
      return 0;
    }
    
    static void reverse ( char *start, char *end )
    {
      char hold;
    
      while ( start < end ) {
            hold = *start;
            *start = *end;
            *end = hold;
    
                 start++;
                 end--;
      }
    }
    Is there anything I can do with the programe. And what should I do to able to achieve the C programming skill like you. (I have seen the other reply you have pose... SUPERB!!!!) Any tips...
    It's the flaws that makes the perfect more perfect....

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Try changing
    Code:
    gets(str);
    to
    Code:
    fgets (str, sizeof str, stdin);
    And
    Code:
    char *last = str + sizeof str;
    to
    Code:
    char *last = str + strlen(str);
    (And spell #include correctly.)
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Mar 2003
    Posts
    10

    Talking quarry...

    fgets (str, sizeof str, stdin);
    why fget is used instead of get?? Will that affected the rest of the function (which I have not pasted it there....)


    char *last = str + sizeof str;
    and why I can't use sizeof.

    Strange enough, when I run the previous programe Perlude produce it work without all these changes... I'm puzzled...

    Sorry to keep bothering.. but just want to know the reason so that I can learn from there..
    Last edited by Isotopes; 03-18-2003 at 03:24 PM.
    It's the flaws that makes the perfect more perfect....

  7. #7
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    >>why fget is used instead of get??
    Dave gave you a link explaining this, look again here

    >>and why I can't use sizeof
    Well, you're trying to get the length of the string, and not how many bytes he takes, so strlen is the correct way to get the length of the string, sizeof operator returns the size of an object in bytes.
    Last edited by Vber; 03-18-2003 at 03:33 PM.

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >why fget is used instead of get??
    It helps to follow the links that try to anticipate your question.

    >and why I can't use sizeof
    Because in your function str is a pointer. In Prelude's code buf was an array. They have different sizes. You want the length of the string, not the size of a pointer to it.

    Actually, I was a little lazy with that last one. I might modify the code like this.
    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    
    static void reverse ( char *start, char *end )
    {
        char hold;
    
        while ( start < end )
        {
            hold = *start;
            *start = *end;
            *end = hold;
    
            start++;
            end--;
        }
    }
    
    char *revstr (char str[])
    {
        char *last = str + strlen(str) + 1;
        char *start = str;
        char *end;
    
        for ( end = start; end != last; start = ++end )
        {
            while ( *end != '\0' && !isspace ( *end ) )
                end++;
    
            reverse ( start, end - 1 );
        }
    
        return str;
    }
    
    int main(void)
    {
        char str[100];
        printf ("Enter the String :");
        fflush(stdout);
        if(fgets (str, sizeof str, stdin) != NULL)
        {
            char *newline = strchr(str, '\n');
            if(newline != NULL)
            {
                *newline = '\0';
            }
            /*[edit] */else
            {
                while(getchar() != '\n');
            } /* [/edit] */
            printf ( "%s\n", revstr (str) );
        }
        return 0;
    }
    
    /* my output
    Enter the String :here is the string to reverse
    ereh si eht gnirts ot esrever
    */
    Last edited by Dave_Sinkula; 03-18-2003 at 03:40 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. How to monitor process creation?
    By markiz in forum Windows Programming
    Replies: 31
    Last Post: 03-17-2008, 02:39 PM
  3. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  5. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM