Thread: print words of a string

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    14

    print words of a string

    I want to print the words of a sentence, given as a string....
    But I have a problem with the end of the sentence, and cannot find the bug....
    Thanks in advance...
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    char str[80]="This is a sentence";
    int main(){
        
        char *temp;
        char *word;
        int i;
        
        word=str;
        temp=word;
        while(temp != NULL){
          while(isalpha(*temp)){
               temp++;
          }//we are in a space character
          i=0;
          //print the word
          while((word+i)!=temp){
             printf("%c",(*(word+i)));getchar();
             i++;
          }
          putchar(*temp);getchar();
          while(isspace(*temp)&&temp!=NULL) temp++;//search for the next alphanumeric character
          
          word=temp;
          printf("*temp=%c %d\n",*temp,*temp);
         }
        getchar();getchar();
        
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    The pointer "temp" does not magically have the value NULL at the end of the string.

    Instead of checking if "temp" is NULL, I'd recommend checking if the value pointed to by "temp" is a null character ('\0'), signifying the end of the string.

    Also, your indentation is good, but I'd suggest using more vertical whitespace. There's no reason to cram two commands on one line (such as on line 20).

    [edit] Oh, and there's no reason to have your character array global. That could, and should, be in "main()".

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    14
    Thanks very much the answer....
    I fixed it...
    But what is the "magic" reason that temp does not have value NULL at the end of the loop?
    You have right for the identation, as for the global function....
    I thought that if str was global temp==NULL will work (lol!!)

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The only way you are changing temp is to move it forward through the string. If you start somewhere and move forward, you can never reach nowhere (which is what NULL is) -- it would require "magic" for that to happen.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    14
    I thought that *p equals '\0' is equivalent to p points to NULL.
    That means, that '\0' is the NULL character, but NOT the NULL pointer (as a pointer).

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    You might have an easier time if you rewrite the loop as follows:

    Code:
    for (temp = str; *temp != '\0'; temp++) {
    ...
    }
    Then, instead of doing "temp++" inside your loop body, do "continue" instead. This way, it's easy to see that each iteration through the loop has a valid char *temp to work with.

    Yes, '\0' has the same integer value as NULL, but notice the comparison:

    Code:
    *temp == NULL
    It does not match in type, because the left-hand side is a char, but the right hand side is a void*

    Code:
    *temp == '\0'
    This way, although equivalent, is better because the types match. Matching types is a good thing in a statically typed language. It helps ensure correctness, for this reason people usually say to turn on all the warnings and never ignore them. Maybe it's a pain at first, but in the end you'll thank the warnings.

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    14
    I see..
    thanks!

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by xiromdim
    I thought that *p equals '\0' is equivalent to p points to NULL.
    No, it isn't: if *p == '\0', then p points to something, hence it is not a null pointer. If p is a null pointer, i.e., p == NULL (which is not really the same as saying that "p points to NULL"), then *p would be wrong since a null pointer cannot be validly dereferenced.

    Quote Originally Posted by xiromdim
    That means, that '\0' is the NULL character, but NOT the NULL pointer (as a pointer).
    Yes.

    Quote Originally Posted by c99tutorial
    Then, instead of doing "temp++" inside your loop body, do "continue" instead. This way, it's easy to see that each iteration through the loop has a valid char *temp to work with.
    This should work, but I suggest that you use continue as a last resort: usually it is possible to just structure your logic such that the rest of the loop body is implicitly skipped without the use of continue, e.g., by negating the condition and having the if statement body contain the part to be skipped.

    Quote Originally Posted by c99tutorial
    It does not match in type, because the left-hand side is a char, but the right hand side is a void*
    Technically, the right hand side might be an int, but it is still a null pointer constant, hence the types do not match from a conceptual point of view.

    Quote Originally Posted by c99tutorial
    This way, although equivalent, is better because the types match.
    Technically, the types are not the same: the left hand side is a char, the right hand side is an int. However, conceptually '\0' is a character constant, even though its type is actually int, so in that sense the types match.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 12-10-2012, 12:24 AM
  2. how to print out words?
    By ecsx00 in forum C Programming
    Replies: 8
    Last Post: 10-12-2011, 12:28 AM
  3. Using do while to print # of words per line...
    By Iron Hide in forum C++ Programming
    Replies: 4
    Last Post: 05-11-2011, 05:25 PM
  4. Replies: 7
    Last Post: 10-01-2010, 04:09 PM
  5. Need help getting program print out the digit in words
    By cosmiccomputing in forum C Programming
    Replies: 26
    Last Post: 04-24-2008, 08:28 AM

Tags for this Thread