Thread: Quick string question

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    19

    Quick string question

    Hi all,
    I have a string that contains a couple words, followed by a number. I need to figure out how to extract that number, and put it into an int variable for computational purposes. I tried using atoi, but that doesn't seem to like the chars before the number. Any ideas?
    TIA

  2. #2
    deep
    Guest
    please try sprintf and sscanf and check out if they work

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    31
    ok, first read the line in with fgets.

    Code:
    #include <string.h> //for strlen()
    
    char line[1024];
    
    fgets(line, sizeof(line), stdin);
    line[strlen(line) - 1] = '\0'; //removes the '\n'
    if the number is always going to be found at
    the end of the string, then you can pass
    strtol() the address of the number located
    within the string, like this:

    Code:
    #include <stdlib.h>  //for strtol()
    
    char *endp;
    
    num = strtol(&line[strlen(line) - 1], &endp, 10);
    heres the prototype for strtol():

    long strtol(char *string, char **endp, int base);

    strtol will attempt to convert the string to whatever
    base you specified and return it as a long int. endp
    will point to '\0' if the entire string was converted
    successfully. if it fails, strtol will return as much of
    the string it could convert and endp will point to the
    first character it couldnt. thats why &line[stren(line) - 1]
    was used instead of just line. by doing that we made sure
    that strtol didnt encounter any of the characters before
    the digit.

    if the number isnt always the last character in the string,
    simply pass strtol &line[strcspn(line, "1234567890")]
    instead of &line[strlen(line) - 1].

    strcspn returns the number of characters skipped until
    it reached one of the characters in the character set.
    that number is used as an index for line.
    make sure you include <string.h>.

    also, you can just use sscanf() like the first guy said.

    Code:
    sscanf(line,"%*[^1234567890] %d", &num);
    the "%*[^1234567890]" portion basically says skip over
    any non-digit characters.

    finally, if you have more than one number within the string,
    you can loop through the string and store all digits in an
    array of int.

    Code:
    #include <ctype.h> //for isdigit()
    
    for(i = 0, j = 0; i < strlen(line); ++i)
    {
       if(isdigit(line[i]))
       {
          iArray[j++] = strtol(&line[i], &endp, 10);
          while(isdigit(line[i])) ++i;
       }
    }
    i suggest you use whatever resources you have to look
    up the string.h functions.

    hope this helps.
    Last edited by CtrlAltKick; 02-15-2002 at 11:54 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  2. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  3. Quick, newbie question: Multiple line string
    By crummy in forum C# Programming
    Replies: 2
    Last Post: 03-10-2005, 06:58 AM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  5. String array question
    By gogo in forum C++ Programming
    Replies: 6
    Last Post: 12-08-2001, 06:44 PM