Dividing a string to words

This is a discussion on Dividing a string to words within the C Programming forums, part of the General Programming Boards category; Hi, I want divide a string (which is composed of words), to words, and I want to keep these words ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    10

    Dividing a string to words

    Hi, I want divide a string (which is composed of words), to words, and I want to keep these words somewhere in memory. To be more clear there is a sentence like:

    Code:
    weather is so warm today
    I need to take each word one by one, and I need to edit them. I can't use any functions from libraries except "stdio.h".

    Could you give me an idea how can I do that?
    Thanks

    bahada

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Well, you need to parse through the string looking for the spaces so that you can split up the string. You can start by just iterating through the string printing out the characters:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        char sentence[] = "weather is so warm today";
        // Put code here to print out the string.  Print it out letter by letter with the putchar() function
        return 0;
    }

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    10
    I'm sorrry but I couldn't understand why would I use putchar(), I don't want to print them as they are, I need to edit them before. Can you give me an idea how can I parse them. Thanks.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    The point is not to print the characters. The point is to access them individually so that you can parse the string. Asking you to print them out with putchar() is just so you know you are parsing through each of the characters.

    I can just give you the code to do it, but that wouldn't teach you anything. Think about the problem for a few minutes. How could you access each character individually? I'll give you another hint:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        char sentence[] = "weather is so warm today";
        char* p = sentence;
        printf("The first character in the string is: %c\n", *p);
        // Put code here to print out the string.  Print it out letter by letter with the putchar() function
        return 0;
    }

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    sscanf is from stdio.h

    %s will give a word %n will give position where parsing stopped, so next sscanf could continue from it
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    Registered User
    Join Date
    Apr 2009
    Posts
    10
    Well, can any of you show me that how can I hold only first letters of these words, tabs and backspaces can exist between words:

    like this is the string:
    weather is so warm today

    and i want to print w, i, s, w, t

    thanks

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Have you made an attempt to solve this yet? If so, post your code and we will help you. No one here is going to do your homework assignment for you though.

  8. #8
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    Do you know that a string in c is really an array of chars. So how would you access an element within an array?

  9. #9
    Registered User
    Join Date
    Mar 2009
    Posts
    30
    This is just silly
    Code:
    #include <stdio.h>
    void main()
    {
            char str[80] = "weather is so warm today" ;
            char *m = str ;
            char word[30] ;
            int x ;
    
            while ( sscanf(m,"%s %n",word,&x) != EOF )
            {
                    printf("%s\n", word) ;
                    m+=x ;
            }
    }
    
    /*  Output
    
    $ ./a.out
    weather
    is
    so
    warm
    today
    
    /*
    Last edited by shiryu3; 04-20-2009 at 05:22 PM.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Way to go moron. Do you see that big list of posts ahead of yours where we DIDN'T DO THEIR HOMEWORK FOR THEM?

    PS: main returns an int.


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

  11. #11
    Registered User
    Join Date
    Mar 2009
    Posts
    30
    Quote Originally Posted by quzah View Post
    Way to go moron. Do you see that big list of posts ahead of yours where we DIDN'T DO THEIR HOMEWORK FOR THEM?

    PS: main returns an int.


    Quzah.
    Well main() returns whatever I want to. I've seen it commonly fly both ways as void and int in various books.

    As for posting a solution I wanted to try it out for myself, I didn't know about the %n trick. The requester can make a choice on learning the programming of turning in something copied. I do recommend learning approach as its tough to advance into the follow up classes copying your way through.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by shiryu3 View Post
    Well main() returns whatever I want to. I've seen it commonly fly both ways as void and int in various books.
    I've seen people drive through red lights. It doesn't make it the right way to drive.


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

  13. #13
    Registered User
    Join Date
    Mar 2009
    Posts
    30
    On my planet a drivers handbook clearly tells us to stop at stop signs and red lights, unless otherwise instructed by a law enforcement officer. However in many C programming books little midget demo programs like 'Hello World' or the code printed above do not have a need to return an int with main()

  14. #14
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Quote Originally Posted by shiryu3 View Post
    On my planet a drivers handbook clearly tells us to stop at stop signs and red lights, unless otherwise instructed by a law enforcement officer. However in many C programming books little midget demo programs like 'Hello World' or the code printed above do not have a need to return an int with main()
    In the C world, we also have a little handbook (called the C standard) which tells us what we can and what we cannot do. That standard tells us main can be defined 2 different ways:
    Code:
    int main(void);
    int main(int argc, char* argv[]);

  15. #15
    Registered User
    Join Date
    Apr 2009
    Posts
    10
    I tried something but I couldn't succeed and printing only first letters is really not a homework of me I just wondered how to do it. I am sorry If I broke the rules and thanks for helping.

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, 06:02 AM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  4. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM
  5. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2003, 11:03 PM

Tags for this Thread


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