Thread: reverse words in a string

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    15

    Unhappy reverse words in a string

    how to change "C is fun!" to "C si !nuf"..

  2. #2
    stewdent
    Guest
    get the size of the string ex: "C is fun!\0" size = 10

    then just loop through the character array starting from the end of the string (less NULL) copying character by character to another array

    then append '\0'

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    15

    reverse

    i need to change from "C is fun!" to "C si !nuf"..

    not to "!nuf si C"

    note the diff?

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    What have you tried so far? Post some code and someone will guide you.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    15
    i tried breaking the words into 3...

    then strrev() them all...but how to break the 3 words and store them...by searching for the space ' '?...

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    strrev() is non-standard, so I don't know if you want to use it or not....

    Personally, I'd do something like this:

    Create a function called reverseme that takes 2 parameters, a char* and an int, prototyped like so:
    >>char *reverseme(char *s, int len);
    Make this function reverse the chars pointed to by s for a length of len. Don't make this function rely on nul terminated strings or anything like that, just make it reverse chars for that length.

    Then make another function, this will drive the reverseme one. Within this function, loop though the input string to find the first occurence of a space. Then call reverseme(), passing it a pointer to the start of the word, and the length of the word. Once reverseme has reversed that word, simply move your pointer past the space, and repeat the process, until you reach \0.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You could always do it the easy way and use recursion...
    Code:
    #include<stdio.h>
    int q( char *u )
    {
    	int z=u==NULL?0:*u=='\0'?0:*u==' '?0:q(u+1)+1;
    	if(z)
    		putchar(*u);
    	return z;
    }
    int main ( void )
    {
    	char*u="C is fun!",*z=u;
    	int a=0;
    	while(z&&*z)
    	{
    		a=q(z);
    		z+=a;
    		if(*z==' ')
    		{
    			putchar(' ');
    			z++;
    		}
    	}
    	return 0;
    }
    Have fun.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Back after 2 years Panopticon's Avatar
    Join Date
    Dec 2002
    Posts
    262
    >> int z=u==NULL?0:*u=='\0'?0:*u==' '?0:q(u+1)+1;

    It hurts my eyes just trying to read that... lol.
    Cant stand professionally coded stuff... im such a noob

  9. #9
    Registered User
    Join Date
    Feb 2003
    Posts
    15

    Talking

    wat does that means..i forgotten...

  10. #10
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Another recursive example:
    Code:
    #include<stdio.h>
    #define SP ' '
    
    char*p(char*t)
    {
       char*s;
       t&&*t&&*t!=SP&&((s=p(t+1))==s)&&putchar(*t);
       return(t&&*t?(*t==SP?t:s):NULL);
    }
    
    int main(void)
    {
       char*t="C is fun!";
       while(t=p(t))putchar(*t++);
    }

  11. #11
    Registered User
    Join Date
    Feb 2003
    Posts
    15

    Smile

    wow..

    i need some explainations on tat...

  12. #12
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    ~

    Code:
    int z=u==NULL?0:*u=='\0'?0:*u==' '?0:q(u+1)+1;
    is the ugly (I mean short ; ) way of writing (more or less)

    Code:
    int z;
    if (u == NULL) z = 0;
    else
    	if (*u == '\0') z = 0;
    	else
    		if (*u == ' ') z = 0;
    		else
    			z = q(u+1)+1;
    "He who makes a beast of himself, gets rid of the pain of being a man." Dr. Johnson

  13. #13
    Registered User
    Join Date
    Feb 2003
    Posts
    10

    Thumbs up

    Hi Monster,

    I appreciate the peice of code you wrote,
    But i think there is no need of so many logical AND's.

    the function p as follows will also work

    Code:
       
       char*p(char*t)
    {
       char*s;
       *t&&*t!=SP&&((s=p(t+1))==s)&&putchar(*t);
       return(*t?(*t==SP?t:s):NULL);
    }
    Beucase the varaible passed to the function p is already initialized. Its not NULL.

    Pls. correct me if i am strong.
    "There is no alternative to consitency..."

  14. #14
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Hi gotyatech,

    Yes in this case you are right, but what if I wrote this function as part of a DLL and someone else called this function with a NULL pointer? Always expect the unexpected...

    b.t.w. the code is crap and a perfect example of bad programming... but you already knew that...

  15. #15
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>You could always do it the easy way and use recursion...
    The recursive solution isn't easy enough to be worth using recursion compared to the iterative solution :-)
    Code:
    #include <stdio.h>
    #include <string.h>
    
    void reverse_substr(char *string, size_t length)
    {
      int start, end;
    
      for (start = 0, end = length-1; start < end; start++, end--)
      {
        char swap = string[start];
        string[start] = string[end];
        string[end] = swap;
      }
    }
    
    char *reverse_words(char *str)
    {
      int length;
      char *p = str, *next;
    
      for (p = str; p != 0; p = strpbrk(p, " "))
      {
        if (*p == ' ')
        {
          p++;
        }
    
        next = strpbrk(p, " ");
    
        length = (next != 0) ? next - p : &str[strlen(str)] - p;
    
        reverse_substr(p, length);
      }
    
      return str;
    }
    
    int main(void)
    {
      char str[] = "C is fun!";
    
      printf("%s\n", reverse_words(str));
    
      return 0;
    }
    *Cela*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM