Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

C - Extracting words from string of ASCII characters

linked list string ascii

  • Please log in to reply
3 replies to this topic

#1 shabu

shabu

    CC Lurker

  • New Member
  • Pip
  • 3 posts

Posted 08 March 2012 - 09:27 PM

Hello,

I'm trying to figure out how to extract a word, defined as a sequence of letters, from a string. I'm fairly new to C and therefore am encountering quite a bit of confusion with pointers.

An example of what I am trying to do: If the string is: ??<Hello...how<are@@you<? I need to extract the words "Hello" "how" "are" and "you", and put the four of them into a linked list. So only letters. Also, whitespace is defined as any character for which isspace() returns a non-zero value.

A word is at most 64 bytes long.

I'm using a linked list and a function read_words(char *s) that takes in the string read in from a file in main using fscanf.
Basically, I want to read in all the strings, and each one read in call read_words() to get the words out of it and into the linked list for further use.

Here is the code I've written, that seems to be throwing an error to do with the pointers:

struct node {
        char *val;
        struct node *next;
        int count;
} *words = NULL;

void read_words(char *s)
{
        struct node *tmp;
        char word[64+1];
        int i, check, wordStarted = 0, count = 0;

        for (i = 0; s[i] != '\0'; i++)
        {
                if ((isspace(s[i]) != 0) || !isalpha(s[i]))
                {
                        if (wordStarted == 1)
                        {
                                check = check_list(word);
                                if (check != 1) {
                                        tmp = malloc(sizeof(struct node));
                                        tmp->val = word;
                                        tmp->count = 1;
                                        tmp->next = words;
                                        words = tmp;
                                }
                                count = 0;
                                wordStarted = 0;
                        }
                }
                else
                {
                        word[count++] = s[i];
                        wordStarted = 1;
                }

        }
}


Any help with this would be greatly appreciated!

Thank you!
  • 0

#2 Soryy708

Soryy708

    CC Resident

  • Banned
  • PipPipPipPip
  • 59 posts

Posted 10 March 2012 - 09:18 PM

Look into strtok() // divides the string into tokens.
  • 0

#3 RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1310 posts
  • Location:C:\Countries\US
  • Programming Language:C, Java, C++, PHP, Python, JavaScript

Posted 10 March 2012 - 09:49 PM

Just a suggestion, this syntax is allowed:
for (i= 0; s[i] != 0; i++){ ...
, at least in C.
  • 0
Regards,
RR

#4 Flying Dutchman

Flying Dutchman

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1090 posts
  • Location:::1
  • Programming Language:C++, Python

Posted 10 March 2012 - 11:01 PM

tmp->val = word;
This doesn't not copy contents of word to val. It assigns address of word to val. Instead you want to use strcpy function.

Also, before you copy word to list, you should place \0 so you copy only the current word and not any remains of previous one (if previous one was longer).
  • 0

The roots of education are bitter, but the fruit is sweet.






Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download