extracting a string from string when a string exist

This is a discussion on extracting a string from string when a string exist within the C Programming forums, part of the General Programming Boards category; Hi.... I want to do following......... I have a string... "service-name-you" I want to get string "you" extracted only if ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    10

    extracting a string from string when a string exist

    Hi....

    I want to do following......... I have a string... "service-name-you" I want to get string "you" extracted only if the "service" is the first item before the first "-"


    following is the code I have so far.....

    Code:
        for (loop = 0; loop < numEntries; loop++)
        {
            strsplit = strstr(entries[loop].name,"services-");
            strsplit = strstr(strsplit,"-");
    
            if((serviceName = strchr(strsplit,'-')) != NULL)
                strcpy(serviceName,serviceName+1);
            LOGINFO("Somiiii restrictions %s",serviceName);
        }
    basically as an example..... entries[loop].name holds string such as....

    "you-me-other"
    "me-all-you"
    "service-name-you"

    numEntries = 3 in this case.

    So when it hit "service-name-you" and has "service" in it I want to extract "you"
    out of it

    Please help me as I am struggling......

    Thanks alot in advance

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    if( sscanf( buf, "%[^-]", word ) == 1 && strcmp( word, "service" ) == 0 )
        ptr = strstr( buf, "you" );
    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Code:
        if( sscanf( buffer, "%*[^-]-%*[^-]-%s", extract ) == 1 )
            printf("%s\n", extract );
    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by ssharish2005 View Post
    Code:
        if( sscanf( buffer, "%*[^-]-%*[^-]-%s", extract ) == 1 )
            printf("%s\n", extract );
    ssharish
    Your example isn't checking to see if the first word is "service".
    Quote Originally Posted by rubu View Post
    I have a string... "service-name-you" I want to get string "you" extracted only if the "service" is the first item before the first

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    I was clearly on a illusion on showing the OP how to extract a sub string froma string. But wasn't intended in giving him a solution. Perhaps i could have included

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    ptr = strrchr( buf, '-' );
    if( ptr )
        sscanf( ptr+1, "%s", word );
    I believe it's your turn.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Quote Originally Posted by quzah
    Your example isn't checking to see if the first word is "service".

    Originally Posted by rubu
    I have a string... "service-name-you" I want to get string "you" extracted only if the "service" is the first item before the first
    How about this

    Code:
        if( sscanf( buffer, "service-%*[^-]-%s", extract ) == 1 )
            printf("%s\n", extract );
    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  8. #8
    Registered User
    Join Date
    May 2011
    Posts
    10
    Thanks so much for helping kindly

    I am commenting on the code so I could try to understand....I do not have C compiler right now so I can't test things.....hope you will understand and help me...

    Code:
    //This basically extract "service" and verify it has service in it. 
    if( sscanf( buf, "%[^-]", word ) == 1 && strcmp( word, "service" ) == 0 )
        ptr = strstr( buf, "you" ); //This pretty much get the pointer to the first char where it find the word you
    but its is little more complected. there can be strings.....where

    "service-name-you"
    "service-name-you1"
    "service-name-you2"
    "service-name-you3"

    I want to extract the last word...."you", "you1", "you2", "you3" when the submitted string has "service".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. extracting char from a string
    By roaan in forum C Programming
    Replies: 12
    Last Post: 07-25-2009, 03:03 PM
  2. extracting from a string.
    By shabbirhussain in forum C Programming
    Replies: 11
    Last Post: 08-10-2008, 02:04 PM
  3. extracting specify int in a string
    By lingz82 in forum C++ Programming
    Replies: 5
    Last Post: 09-06-2006, 10:18 AM
  4. extracting function out of a string
    By HeLlsHinE in forum C Programming
    Replies: 3
    Last Post: 06-15-2005, 10:52 AM
  5. Extracting a sub-String
    By mikem22 in forum C Programming
    Replies: 1
    Last Post: 02-03-2003, 07:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21