Thread: Arrays of Words

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    21

    Arrays of Words

    I'm trying to work on a program that takes a string as an input, parses it into words according to spaces and newline characters, puts each word in its' own space into a char array, then prints the array. However, what I've got so far only prints garbage characters past the first two or so characters.

    This:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main ()
    {
            char wordlist[100][100];
            char templist[] = "This is a bunch of words";
            char *tokenptr;
    
            int wordcnt = 0;
    
            tokenptr = strtok(templist, " ");
            while (tokenptr != NULL)
            {
                    if (tokenptr != " ");
                    {
                            *wordlist[wordcnt] = *tokenptr;
                    }
                    if (tokenptr == " ");
                    {
                            wordcnt++;
                    }
                    tokenptr = strtok(NULL, " ");
            }
            for (wordcnt = 0; wordcnt < 100; wordcnt++)
            {
                    printf("&#37;s", wordlist[wordcnt]);
            }
    }
    outputs this:
    Code:
    Tiabow&#255;=&#246;&#255;=&#246;&#172;&#255;=&#246;&#255;=&#246; &#255;?B@&#255;?@0&#255;;&#193;0&#255;&#191;&#239;&#255;?@&#184;&#255;?B@&#255;?U&#176;&#255;?@0&#255;&#191;&#240;,&#255;?{p&#255;&#191;&#237;&#168;
    &#255;?I&#255;?{p&#166;)&#227;&#255;2&#234;&#248;&#255;&#191;&#238;L&#255;?@&#252;&#255;?&#255;3&#224;P&#255;5n&#255;6&#253;&#255;9 &#255;6&#178;&#180;&#255;?{p&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;
    &#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;
    &#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;&#255;;
    Any idea what I'm doing wrong here?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Lot's of if statements with dubiously placed ;'s

    Lots of trying to assign strings with = and compare them with ==
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    21
    Ok, new problem. The old one was just me being stupid and unobservant.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main ()
    {
    
            char wordlist[100][100];
            char templist[1000];
            char *tokenptr;
    
            int wordcnt = 0, cnt;
    
            scanf("%s", &templist);
    
            tokenptr = strtok(templist, " ");
            while (tokenptr != "0")
            {
                    strcat(wordlist[wordcnt], tokenptr);
                    tokenptr = strtok(NULL, " ");
                    wordcnt++;
            }
    
            for (cnt = 0; cnt < wordcnt; cnt++)
            {
                    printf("%s ", wordlist[cnt]);
            }
    }
    When executed, it's supposed to read input from input.txt using I/O redirection.
    Input.txt:
    Code:
    This is a test.
    0
    However, when I execute, all I get is a seg fault. I tried to change it from tokenizing when it hits NULL instead of a 0 in the string, but the change there is that it only read up to 'This' of input.txt.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > scanf("&#37;s", &templist);
    1. You don't need the & when using %s and a char array.
    2. %s stops at the first space anyway, so there is nothing to tokenise.
    Use
    fgets( templist, sizeof templist, stdin );

    > while (tokenptr != "0")
    "0" is NOT the same as NULL

    > strcat(wordlist[wordcnt], tokenptr);
    Try strcpy() instead.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    21
    Quote Originally Posted by Salem View Post
    "0" is NOT the same as NULL
    What I was trying to do there is have it tokenize until it reaches a '0' in the string (The string will consist only of alphanumeric characters).

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So first you test for NULL, then you use say strcmp() to compare for a specific string.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My Arrays!!!, My Arrays!!! Help!?
    By llcool_d2006 in forum C++ Programming
    Replies: 1
    Last Post: 12-10-2006, 12:07 PM
  2. I'm not THAT good am I?
    By indigo0086 in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-19-2006, 10:08 AM
  3. Dynamic (Numeric) Arrays
    By DavidB in forum C++ Programming
    Replies: 5
    Last Post: 05-03-2006, 07:34 PM
  4. Problem with malloc() and sorting words from text file
    By goron350 in forum C Programming
    Replies: 11
    Last Post: 11-30-2004, 10:01 AM
  5. extracting words from an array of words
    By axon in forum C++ Programming
    Replies: 2
    Last Post: 04-02-2003, 11:21 PM