Thread: retreive part of a string

  1. #1
    Unregistered
    Guest

    retreive part of a string

    Im a newbie in programming.
    How do you retreive part of a string in a c program?

    eg.
    char mytext[] = "abcdef[12345]";

    I want to retreive the value inside the []...

    Please help.

  2. #2
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    mytext[0] will refer to the first character in the string, and so on... you can use functions in string.h such as strcpy, strcmp and strstr to manipulate and look through them.

    http://www.cplusplus.com/ref/cstring/
    you might find that link handy.
    .sect signature

  3. #3
    Unregistered
    Guest
    it still sounds difficult to me.


    I tried, but it doesnt work.... 8(
    line[]="12345";

    lineLen = strlen(line);
    printf("%s\n", line);

    pos = 1;
    while (pos != lineLen)
    {
    strcat(output, line[pos]);
    pos++;
    }

  4. #4
    Registered User moonwalker's Avatar
    Join Date
    Jul 2002
    Posts
    282

    looks like

    looks like you did not declare "output"

  5. #5
    Unregistered
    Guest
    I have decalre output

    #define MAXSIZE 30000

    ...
    ...
    char output[MAXSIZE];

    aww....
    anyone show me a good function to retreive a part of a string.

  6. #6
    Registered User
    Join Date
    Jul 2002
    Posts
    66
    anyone show me a good function to retreive a part of a string.
    It depends on what you need to retrieve and how variable it is. If it's the same thing with every string then the strstr() function is ideal, but if it's in between two delimiting characters you'll have a harder time of things. The formatting of the string is also important and it must remain constant if you want your code to work consistently.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define OPEN  '['
    #define CLOSE ']'
    
    int main (void)
    {
      char mytext[] = "abcdef[12345]";
      char *Start = strchr (mytext, OPEN);
    
      if (Start){
        while (*Start++){
          if (*Start == CLOSE)
            break;
          /* Do what you need to with the data,
             save it to another string or process
             it as is. This part is up to you.
          */
        }
      }
      return 0;
    }

  7. #7
    Unregistered
    Guest
    cool

    thanks dude

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    And just for fun, here's another version of the same theme...
    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define OPEN    '['
    #define CLOSE   ']'
    
    int main(void)
    {
        char    mytext[] = "abcdef[12345]";
        char    *Start;
        char    *End;
    
        if ((Start = strchr(mytext, OPEN)) != NULL && ((End = strchr(Start, CLOSE))) != NULL)
        {
            /* Now Start points to the [ and End points to the ] */
    
            /* Here's the proof: */
            char    buf[BUFSIZ];
            int     Len = End - Start + 1;
            strncpy(buf, Start, Len);
            buf[Len] = '\0';
            printf("Buf contains: %s\n", buf);
        }
    
        return(0);
    }
    This uses 2 strchr() calls to find the start and end markers, and then determines the distance between them. And finally, it prints the proof that it's worked.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Registered User
    Join Date
    Jul 2002
    Posts
    66
    Code:
    if ((Start = strchr(mytext, OPEN)) != NULL && ((End = strchr(Start, CLOSE))) != NULL)
    No offense Hammer, but I have a little bit of a problem with this line. Nothing serious, it just does too much for one statement and can be confusing to a reader. I feel, and this is just me, that it would be better if the line were broken up into several logical lines.
    Code:
    Start = strchr (mytext, OPEN);
    End   = strchr (Start, CLOSE);
    if (Start && End)

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Crimpy
    Code:
    if ((Start = strchr(mytext, OPEN)) != NULL && ((End = strchr(Start, CLOSE))) != NULL)
    No offense Hammer, but I have a little bit of a problem with this line. Nothing serious, it just does too much for one statement and can be confusing to a reader. I feel, and this is just me, that it would be better if the line were broken up into several logical lines.
    Code:
    Start = strchr (mytext, OPEN);
    End   = strchr (Start, CLOSE);
    if (Start && End)
    No offense taken, I've been around too long to get annoyed at people disliking my code

    Anyway, yes, I can understand that line is longer than average for a newbie, but I don't believe an experienced programmer would have any problem understanding it.

    Your suggested correction fails to take into account the fact that the first call the strchr() might not find an opening marker ([ in this case), and therefore the program will crash and burn on the second call, with a NULL pointer being passed to it.

    There are of course, many ways in which you could rearrange these statements to make them more friendly to the newbie, and one of the simplest is this:
    Code:
    if ((Start = strchr(mytext, OPEN)) != NULL 
    && ((End = strchr(Start, CLOSE))) != NULL)
    This is still using the same single if statement, but being over 2 lines, it aids reading.

    And of course, a comment or two wouldn't go a miss in a real prog
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    Registered User alex's Avatar
    Join Date
    Sep 2001
    Posts
    132
    Just another variation on the same theme...

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        char mytext[] = "abcdef[12345]";
        char number[20];
        int test;
    
        /* sscanf: throw away all up to "["; read "["; 
              and then read characters up to "]" */
        test = sscanf(mytext, "%*[^[]" "[" "%[^]]", number);
        if(test==1) /* sscanf should have read read exactly one field */
        {
           printf("The answer is: %s\n", number);
        }
    
        return 0;
    }
    there is one (minor) flaw here: this code does not check whether the "]"-character really exists in the input string. "%[^]]" stops reading characters if a "]"-character is found, OR if the end of the string is reached.

    alex

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. problems with overloaded '+' again
    By Brain Cell in forum C++ Programming
    Replies: 9
    Last Post: 04-14-2005, 05:13 PM
  5. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM