Thread: String break to words and numbers

  1. #1
    Registered User
    Join Date
    Oct 2001

    String break to words and numbers

    I am trying to write a function to read data from an input file (specific format of the input file is as follows - each line is of the form: name score). Then, I want to break the string into a character string part and a number part, then perform manipulation on each. The name may contain any number of words with any amount of white space between them. No word in a name may be a number. The score is a floating point number.

    Example sample data (taken from different data sets to illustrate different types of input data):

    Cleveland .656
    Nagy, Clev 227.0
    President Mr. John Adams -71.86
    The Wonderful World of Disney +0.101

    I use fgets to read in the string (learned the hard way that scanf doesn't work well here). How do I test when a character string stops and the number begins? Would I have to test each character for a number or a special character followed by a number(-1, .5, +0)?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    You could try using strpbrk

    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Oct 2001

    Thumbs up Worked perfectly!

    Using strpbrk was exactly what I needed. I'll need to review more of those string.h functions. Thanks!

  4. #4
    Registered User
    Join Date
    Oct 2001

    Smile Resolution

    I think I've finished it. At least it accepts the data sets that I have without errors. Here's the code...


    char myString[100], *myScore;
    double numScore;

    while (fgets(myString, 100, stdin))
    printf("\n%s", myString);
    myScore = strpbrk(myString, ".-0123456789");

    while(myScore[0] == '.' && (myScore[1] < '0' || myScore[1] > '9'))
    myScore = strpbrk(myScore+1, ".-0123456789");
    if(myScore[0] == '.') numScore = 0.1 * atof(myScore+1);
    if(myScore[0] == '-') numScore = -1 * atof(myScore+1);
    else numScore = atof(myScore);

    printf("The score is: %lf", numScore);


    It skipps over periods if their in a name or title, but accepts them if they are part of a number. Is there a more efficient way to do this, or is this about as good as it gets? Thanks again for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  3. Switching two words around in a string
    By NewbGuy in forum C Programming
    Replies: 3
    Last Post: 06-21-2008, 04:32 AM
  4. ascii rpg help
    By aaron11193 in forum C Programming
    Replies: 18
    Last Post: 10-29-2006, 01:45 AM
  5. program to convert numbers into words
    By Kingsley in forum C Programming
    Replies: 5
    Last Post: 07-01-2006, 07:50 PM