Thread: beginner question about strings

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    60

    beginner question about strings

    Hey, new here
    I have a problem.

    I need to read in a string of chars (upper/lower case letters and punctuation marks : '.' or ',' or ';' or '?' or '!') of unspecified length, then take each one of the words and process some calculations such as : average length of a word, average vowels in each word, how many words are between 10 and 15 letters long etc..

    An example input would be:
    Hello there! How are you?

    I read a string in using the keyboard and I use strtok from the <string.h> library to break the string down to individual words. The problem is that the words in the string can be seperated by any white space characters not just by the spacebar.
    Iam using something like :
    char line[MAX_SIZE];
    strtok( line, " ");
    so I can break the string into words. But I am only limited to words that are seperated by the spacebar " ".
    How do I take each word seperately from the input string and run the calculations on it ?


    Thanks in advance
    gp

  2. #2
    Registered User
    Join Date
    Aug 2008
    Posts
    67
    Quote Originally Posted by gp364481 View Post
    Hey, new here
    I have a problem.

    I need to read in a string of chars (upper/lower case letters and punctuation marks : '.' or ',' or ';' or '?' or '!') of unspecified length, then take each one of the words and process some calculations such as : average length of a word, average vowels in each word, how many words are between 10 and 15 letters long etc..

    An example input would be:
    Hello there! How are you?

    I read a string in using the keyboard and I use strtok from the <string.h> library to break the string down to individual words. The problem is that the words in the string can be seperated by any white space characters not just by the spacebar.
    Iam using something like :
    char line[MAX_SIZE];
    strtok( line, " ");
    so I can break the string into words. But I am only limited to words that are seperated by the spacebar " ".
    How do I take each word seperately from the input string and run the calculations on it ?


    Thanks in advance
    gp
    Are you saying one of your problems are words like these: there!, you?

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    60
    My problem is input like this:

    Hello [tab] there how are [tab] you doing?

    because strtok( string, " "); takes a space as an arguement, my program will break down the string everytime it finds a single space as follows:

    Hello [tab] there
    how
    are [tab] you
    doing?

    I want it to be:
    Hello
    there
    how
    are
    you
    doing?

    thanks

    Edit: i dont know how to display tabbed text in the forums so where u see [tab] it means the user hit the tab button
    Last edited by gp364481; 09-05-2008 at 06:22 PM.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can put multiple things in your tokenizer string. You might want something like " \t\n".

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    60

    Wink

    Quote Originally Posted by tabstop View Post
    You can put multiple things in your tokenizer string. You might want something like " \t\n".
    thanks! u saved the day

    ...for now ;0

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-29-2009, 10:13 AM
  2. beginner: dynamic array of strings
    By pc2-brazil in forum C++ Programming
    Replies: 10
    Last Post: 04-29-2008, 04:29 PM
  3. Retarded (most likely) question about strings.
    By Kris in forum C Programming
    Replies: 11
    Last Post: 10-13-2005, 11:30 AM
  4. Question on malloc() and reading strings
    By rockysfr in forum C Programming
    Replies: 2
    Last Post: 09-04-2005, 06:37 PM
  5. question about strings!
    By aspand in forum C Programming
    Replies: 1
    Last Post: 05-16-2002, 05:39 AM