Thread: a problem about string

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

    Question a problem about string

    after i get a string from the user, how can i break the string into words??
    likes
    the string" i am very happy today."
    to
    i
    am
    very
    happy
    today

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    If you don't require soemthing reentrant, strtok is a standard function:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define DELIM " "
    
    int main(void)
    {
        char buff[BUFSIZ];
    
        printf("Enter a string: ");
        fflush(stdout);
    
        if (fgets(buff, sizeof buff, stdin) != NULL) {
            char *sep = strtok(buff, DELIM);
    
            while (sep != NULL) {
                puts(sep);
                sep = strtok(NULL, DELIM);
            }
        }
    
        return 0;
    }
    If you do need a reentrant function, you'll have to write it yourself or use a nonstandard extension if your compiler supports one.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    10
    sorry, i havent typed it down that the string is stored in the 2D array having a maximum 80 words....

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >sorry, i havent typed it down that the string is stored in the 2D array having a maximum 80 words....
    So copy each token into the array. Something like this:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define DELIM   " "
    #define MAXWORD 80
    #define MAXLEN  20
    
    int main(void)
    {
        char words[MAXWORD][MAXLEN];
        char buff[BUFSIZ];
        int  ntokens = 0;
        int  i;
    
        printf("Enter a string: ");
        fflush(stdout);
    
        if (fgets(buff, sizeof buff, stdin) != NULL) {
            char *sep = strtok(buff, DELIM);
    
            while (sep != NULL) {
                strcpy(words[ntokens++], sep);
                sep = strtok(NULL, DELIM);
            }
        }
    
        for (i = 0; i < ntokens; i++)
            puts(words[i]);
    
        return 0;
    }
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    10
    THx u very much
    by the way
    i am a beginner of c, many times find many difficulty in writing programs, how can i improve my programming skills?

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >how can i improve my programming skills?
    Read lots of good code and try to imitate what you read. Eventually you'll find yourself writing good code with ease.
    My best code is written with the delete key.

  7. #7
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >how can i improve my programming skills?
    Taking an English course would also help.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  4. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  5. Replies: 4
    Last Post: 03-03-2006, 02:11 AM