reverse the words sorting in a string

This is a discussion on reverse the words sorting in a string within the C Programming forums, part of the General Programming Boards category; hi all i am trying to code somthing do this reverse life is nightmare then prints like this nightmare is ...

  1. #1
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127

    reverse the words sorting in a string

    hi all i am trying to code somthing do this

    reverse
    life is nightmare

    then prints like this

    nightmare is life

    here is my code but it's actually gives me a memory leak and not working propaply
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    #define BUF 250
    
    main()
    {
     char buffer[BUF],word[BUF],*rev;
     int x,g,len;
     
     fgets(buffer,BUF,stdin);
     len=strlen(buffer);
     
     if((rev=malloc(len))==NULL)
     {
                         fprintf(stderr,"Memory Allocating Error\n");
                         exit(1);
     }
          
     rev[len]='\0';
     
        for(x=len-1,g=0;x>=1;x--,g++)
          {
               word[g]=buffer[x];
                                      switch(buffer[x])
                                      {
                                                       case ' ':
                                                            word[g]='\0';
                                                           strcat(rev,strrev(word));
                                                            g=0;
                                                            break;
                                      }
          }
          
          printf("&#37;s",rev);
    
    return 0;
    }
    thanks in advance
    and there is smothing else
    i cannot do this by looking for spaces but i didn't fine any idea right now
    thanks

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Why can't you do this based upon spaces?

    And what is the actual question?

  3. #3
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    More to the point, learn how to indent it'll make reading it possible

  4. #4
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127
    Quote Originally Posted by MacGyver View Post
    Why can't you do this based upon spaces?

    And what is the actual question?
    Why can't you do this based upon spaces?
    because it will only add the '\0' if it finds spaces

    And what is the actual question?
    making a string like this "storm man " be like this "man storm"

  5. #5
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127
    Quote Originally Posted by zacs7 View Post
    More to the point, learn how to indent it'll make reading it possible
    i will

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by St0rM-MaN View Post
    because it will only add the '\0' if it finds spaces
    Then you're doing it wrong.

    Quote Originally Posted by St0rM-MaN View Post
    making a string like this "storm man " be like this "man storm"
    That's not a question. Try again.

  7. #7
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127
    Quote Originally Posted by MacGyver View Post
    Then you're doing it wrong.
    that's what i've said and i think i found a solution i can make word alrady closed like rev for exaple like this word[len]='\0';
    then continue in my work and if i found the space i use strcat to copy it to the rev buffer
    is this o.k ? ofcourse i will try it my self but i can't now
    That's not a question. Try again.
    you are killing me but i like that
    any way
    for example here is a string
    "I Am So Lazy"
    all what i need to resort the words in reverse order by copying it to another buffer called revlike that
    "Lazy So Am I"
    and i've coded the code already but it's not working as it suppose to
    "i hop i asked the right question"

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by St0rM-MaN View Post
    you are killing me but i like that
    any way
    for example here is a string
    "I Am So Lazy"
    all what i need to resort the words in reverse order by copying it to another buffer called revlike that
    "Lazy So Am I"
    and i've coded the code already but it's not working as it suppose to
    "i hop i asked the right question"
    You still didn't ask a question. You're just restating your intentions, and attempting to implicitly ask for an answer. I'm not going to give you that answer under these circumstances.

  9. #9
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    The solution for this problem is so simple. And you have made it so complicated with all those switch and while and all. What you need a is Tokenization and 2D char array and thats it. Here is a pesudo code

    1. Tokenize the string with the space as your deelimiator.
    2. You get 2D char array.
    for example:
    Code:
    This is a test
    
    str[0]  = "This"
    str[1]  = "is"
    str[2]  = "a"
    str[3]  = "test"
    That is what u get. Print the 2D array reverse. That is str[3] str[2].. str[0].

    And thats it u get the answer. And thats what MacGyver is trying to explain.

    ssharish2005

  10. #10
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127
    Quote Originally Posted by MacGyver View Post
    You still didn't ask a question. You're just restating your intentions, and attempting to implicitly ask for an answer. I'm not going to give you that answer under these circumstances.
    i am not doing this but i can't really find the question
    could you pleas tell me how to ask for a question like this?
    because i am really confused about asking the right question and i am talking honestly

    The solution for this problem is so simple. And you have made it so complicated with all those switch and while and all. What you need a is Tokenization and 2D char array and thats it. Here is a pesudo code
    you mean copy words into a 2d array ?
    and just reverse the order of print ?
    ooooh i got it i will code it and try thanks

  11. #11
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    you mean copy words into a 2d array ?
    and just reverse the order of print ?
    ooooh i got it i will code it and try thanks
    Yes

    ssharish2005

  12. #12
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Usually when you ask a question, you use a question mark for a start
    Read: http://www.catb.org/~esr/faqs/smart-questions.html

  13. #13
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127
    Quote Originally Posted by zacs7 View Post
    Usually when you ask a question, you use a question mark for a start
    Read: http://www.catb.org/~esr/faqs/smart-questions.html

    for example like this ->

    i have a coded a program that do the following
    takes a string as input from user
    save it to an array called buffer
    and then the program copy the words from buffer in reverse order to another array called rev
    for example

    Enter a string :
    >"Is This Way OK"
    --------------------------------------------------------------------------------------------------------------
    buffer[BUFSIZ]="Is This Way OK" copy it to another array rev[BUFSIZ]="OK Way This IS"
    --------------------------------------------------------------------------------------------------------------
    output:
    >"OK Way This Is"
    but it's not working so
    what am i doing wrong? and is there is another way of doing this ?
    ---------------------------------------------------------------------------------------------------------------
    i am really trying to learn i hope i am not bothering you

  14. #14
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127
    Code:
    #include <stdio.h>
    #include <stdio.h>
    
    #define BUF 250
    #define WSIZE 250
    
    int main(void)
      {
                  char buffer[BUF];
                  char **rev;
                  char word[WSIZE];
                  
                  int x,g,f;
                  
                 fgets(buffer , BUF , stdin);/* take user input*/ 
                 
                 for(x=0 , g=0 , f=0; buffer[x]!='\0' ; x++ , f++)
                 {
                              word[f]=buffer[x];
                              
                              if(buffer[x]==' ')
                              {
                                 word[f]='\0';
                                 rev[g]=strdup(word);
                                 f=0;             
                                 g++;
                              }                                          
                 }
                 
                   
                   word[f]='\0';
                   rev[g]=strdup(word);
                   
                                for(x=g ; x>=0 ;x--)
                                {
                                  printf("%s",rev[x]);
                                }
        
            return 0;
        }
    this code is not working at all
    but i've replaced this char **rev; with char *rev[3];
    int this code
    Code:
    #include <stdio.h>
    #include <stdio.h>
    
    #define BUF 250
    #define WSIZE 250
    
    int main(void)
      {
                  char buffer[BUF];
                  char *rev[3];
                  char word[WSIZE];
                  
                  int x,g,f;
                  
                 fgets(buffer , BUF , stdin);/* take user input*/ 
                 
                 for(x=0 , g=0 , f=0; buffer[x]!='\0' ; x++ , f++)
                 {
                              word[f]=buffer[x];
                              
                              if(buffer[x]==' ')
                              {
                                 word[f]='\0';
                                 rev[g]=strdup(word);
                                 f=0;             
                                 g++;
                              }                                          
                 }
                 
                   
                   word[f]='\0';
                   rev[g]=strdup(word);
                   
                                for(x=g ; x>=0 ;x--)
                                {
                                  printf("%s",rev[x]);
                                }
        
            return 0;
        }
    and it works but i don't want to do this as i don't know how many words will be inputed!!
    and take a look at this

    Enter A String:

    ->Storm Man
    (output)
    sman
    storm


    first of all "sman" why did it copy the first letter in the string ?
    second it prints new lines between the words???
    i don't get this point also why did it do this ?
    thanks in advance and i hope i asked the right question

  15. #15
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Code:
    #include <stdio.h>
    
    #define BUF 250
    #define WSIZE 250
    
    int main(void)
    {
        char buffer[BUF];
        char *rev[3];
        char word[WSIZE];
        int x,g,f;
                  
        fgets(buffer,BUF,stdin);
                 
        for(x=0 , g=0 , f=0; buffer[x]!='\0' ; x++ )
        {
             word[f++]=buffer[x];
                             
             if(buffer[x]==' ')
             {
                 word[f]='\0';
                 rev[g]= strdup(word);
                 f=0;             
                 g++;
             }                                          
        }
                   
        word[f]='\0';
        rev[g]= strdup(word);
                   
        for(x=g ; x>=0 ;x--)
         printf("&#37;s\n",rev[x]);
         
        getchar();  
        return 0;
    }
    /* my output
    Why dont u use strtok
    strtok
    use
    u
    dont
    Why
    */
    ssharish2005

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 07:02 AM
  2. how to extract words from a string
    By panfilero in forum C Programming
    Replies: 7
    Last Post: 11-04-2005, 08:06 AM
  3. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 11:32 AM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 10:37 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21