#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2006
    Posts
    12
    Rep Power
    0

    Pulling specific characters from string in C


    i want to be able to input say a 16 char long string and have the program pull out characters 12-14 and put them into another string. any ideas? thank you.
  2. #2
  3. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2004
    Posts
    1,676
    Rep Power
    133
    Same as the last times you asked. [edit]Oh, wait -- C.[/edit]
    http://forums.devshed.com/c-programm...rs-363406.html
    http://forums.devshed.com/c-programm...ng-363732.html

    Why don't you just post an example of a couple of lines of actual input and your expected output?

    [edit]This last request still stands. It will be far easier if you just cut to the chase.
    Last edited by Dave Sinkula; June 29th, 2006 at 01:44 PM.
    Any advertisement triggered by IntelliTxt in this post is not endorsed by the author of this post.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2006
    Posts
    12
    Rep Power
    0
    yes sorry. i should have specified. i need this in C.
    input string: "I ate 47 hot dogs"
    output string;"47"
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    Originally Posted by mfc51
    i want to be able to input say a 16 char long string and have the program pull out characters 12-14 and put them into another string. any ideas? thank you.
    Code:
    // assuming you were using 1-based indexing and not the normal 0-based
    strncpy(sSubStr,&sInStr[11],3);  
    sSubStr[3] = '\0';
    Or you could do it char-by-char in a for-loop.

    PS
    Just for S&G's:
    Code:
    char *my_strcpy(char *sDest, const char *sSrc)
    {
        char *sp, *dp;
    
        sp = sSrc;
        dp = sDest;
    
        while (*sp != '\0')
            *dp++ = *sp++;
    
        *dp = *sp;
    
        return sDest;
    }
    Last edited by dwise1_aol; June 29th, 2006 at 02:10 PM.
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    Originally Posted by mfc51
    yes sorry. i should have specified. i need this in C.
    input string: "I ate 47 hot dogs"
    output string;"47"
    You should be able to very easily modify what I had given you.


    PS
    Please note that your example does not fit your own specification.

    You had specified a 16-character string. Your example has 17.

    You had specified copying out three characters starting with the twelvth. Your example is for two characters starting with the seventh (id est, with the char indexed by 6).

    Now, by this new specification, if you change the input string thus:
    input string: "I had eaten 47 hot dogs"
    the output string should be thus:
    output string;"ea"

    If that is not what you want, then you need to correct your specification.
    Last edited by dwise1_aol; June 29th, 2006 at 02:24 PM.
  10. #6
  11. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2004
    Posts
    1,676
    Rep Power
    133
    Originally Posted by mfc51
    yes sorry. i should have specified. i need this in C.
    input string: "I ate 47 hot dogs"
    output string;"47"
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       char text[] = "I ate 47 hot dogs";
       int num;
       if ( sscanf(text, "%*[^+-0123456789]%d", &num) == 1 )
       {
          printf("num = %d\n", num);
       }
       return 0;
    }
    
    /* my output
    num = 47
    */
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       char *text[] = 
       {
          "I ate 47 hot dogs",
          "I have eaten 100!",
       };
       int num;
       size_t i;
       for ( i = 0; i < sizeof text / sizeof *text; ++i )
       {
          if ( sscanf(text[i], "%*[^+-0123456789]%d", &num) == 1 )
          {
             printf("num = %d\n", num);
          }
       }
       return 0;
    }
    
    /* my output
    num = 47
    num = 100
    */
    Last edited by Dave Sinkula; June 29th, 2006 at 02:17 PM.
    Any advertisement triggered by IntelliTxt in this post is not endorsed by the author of this post.

IMN logo majestic logo threadwatch logo seochat tools logo