Thread: need help understanding this simple c code.

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    5

    Question need help understanding this simple c code.

    This is a simple program to reverse the words in a sentence:

    I am a newbie in C and I would definitely appreciate some help.

    Code:
    #include <stdio.h>
    #define MAXLINE 100
    
    static void reverse(char *str)
    {
    	char word[MAXLINE];
    	char *w = word;
    	while (*str == ' ')
    		 ++str;
    	if ( *str == '\0' ) 
    		return;
    	while (*str != '\0' && *str != ' ') 	
    		*w++ = *str++;
    	*w = '\0';
    	reverse(str);
    	printf("%s ",word);
    }
    
    int main ( )
    {
    	char str[MAXLINE];
    	printf("Enter a sentence:\n");
    	fflush(stdout);
    	gets(str);
    	reverse(str);
    	return 0;
    }
    I got this code from a website and i couldn't understand the reverse function especially the first while stmt and the if stmt.

    Thanks in advance.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    while not at a space

    and

    if end of the string



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

  3. #3
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    This is working correctly whatz the problem with the code it is working fine first of all
    did u compiled at your end with gcc or any compiler....

    do u want to understand the code logic or what ???

    Code:
    while (*str == ' ')
    		 ++str;
    in this code str will reach at that point where
    ++str will be done until the str != ' ' space occur

    Code:
    	if ( *str == '\0' ) 
    		return;
    means if str is having '\0' Null termination character then return
    '\0' is always appends at the end of every string , which treats as the string end.

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    5
    Quote Originally Posted by RockyMarrone View Post
    This is working correctly whatz the problem with the code it is working fine first of all
    did u compiled at your end with gcc or any compiler....

    do u want to understand the code logic or what ???

    Code:
    while (*str == ' ')
    		 ++str;
    in this code str will reach at that point where
    ++str will be done until the str != ' ' space occur

    Code:
    	if ( *str == '\0' ) 
    		return;
    means if str is having '\0' Null termination character then return
    '\0' is always appends at the end of every string , which treats as the string end.
    I know the program works fine. I just couldn't understand the logic. that was pretty obvious from the while statement that ++str will be done until the str != ' ' space occur..
    But my question was why!!??! Am sorry to bother you but would you mind explaining the same?

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    #include <stdio.h>
    #define MAXLINE 100
    
    static void reverse(char *str)
    {
    	char word[MAXLINE];
    	char *w = word;
    	while (*str == ' ')
    		 ++str;         //let's str skip over white space.
    	if ( *str == '\0' )   //str has found the end of string char, we're done here.
    		return;
    	while (*str != '\0' && *str != ' ') //if str is pointing at a letter, not a space, and	
    		*w++ = *str++;              //not an end of string char, then
    	*w = '\0';                          //we've got another w, and increment str, please! 
    	reverse(str)                        //we're not at the end, more words to follow, so
    	printf("%s ",word);                 //mark this word just found, with an end of string char
    }                                            //and print it.
    
    int main ( )
    {
    	char str[MAXLINE];
    	printf("Enter a sentence:\n");
    	fflush(stdout);
    	gets(str);
    	reverse(str);
    	return 0;
    }
    Hope the above comments help. Str is a pointer, and you can add to pointers, to "walk" through data. The pointer will know the size of the thing it's pointing to, so it will be sized right for the job.
    Last edited by Adak; 10-10-2009 at 12:32 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 14
    Last Post: 11-23-2005, 08:53 AM
  2. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Windows Programming
    Replies: 0
    Last Post: 10-14-2002, 01:29 PM
  3. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 12:51 PM
  4. Simple Code, looking for input.
    By Alien_Freak in forum C Programming
    Replies: 3
    Last Post: 03-03-2002, 11:34 AM