Thread: strchr()

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    91

    strchr()

    using the strchr() is to see if a particular character is present in a string. Is it possible to find exactly where this character is in the string?

    so say i have
    Code:
    char word[100];
    word = "hello there re-tard"
    strchr(word,'-');
    is it possible to find at what location the '-' is without doing something like the following

    Code:
    for(i=0;i<strlen(word);i++)
    {
        if(word[i] == '-')
            return i;
    }

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    sure is:
    Code:
    char word[] = "hello there re-tard"; /* Note the way you have it in your example
                     will not work */
    char *ptr;
    int index = -1;
    ptr = strchr(word, '-');
    if ( ptr != NULL )
      index = ptr - word;
    else
      puts("Character not found");

  3. #3
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    pointer math
    Code:
    char * p;
    int index;
    char word[10] = "hi-there!";
    
    p = strchr(word, '-');
    if(p)
        index = p - c;
    edit:
    Thantos beat me, no surprise

  4. #4
    Registered User
    Join Date
    Jul 2004
    Posts
    91
    when you do that, does that return the first occrurence of "-" ?
    hi-there re-tard
    so the "-" will be at position 2 ?

  5. #5
    Quote Originally Posted by paperbox005
    when you do that, does that return the first occrurence of "-" ?
    hi-there re-tard
    so the "-" will be at position 2 ?
    What does your C-book or help file say about strchr() ?

    Google 'man strchr()' gives:
    Quote Originally Posted by Google
    The strchr() function returns a pointer to the first
    occurrence of the character c in the string s.
    We are not going to comment all the standard C-functions. You must have some reference documentation.

    I recommend

    http://www.dinkumware.com/refxc.html
    Emmanuel Delahaye

    "C is a sharp tool"

  6. #6
    Registered User
    Join Date
    Jul 2004
    Posts
    91
    ok well im trying to do this..
    hi-there what-is-your-name
    and using strchr to check if there is a "-" in the string.
    so in this case it would point to the first "-" btw the "hi" and "there"
    the next time i call strchr() how is it possible for me to make it point to the "-" that is btw the "what" and "is" ? if that is even possible?

  7. #7
    Registered User
    Join Date
    Jul 2004
    Posts
    91
    Originally Posted by Google

    The strchr() function returns a pointer to the first
    occurrence of the character c in the string s.
    i just found somewhere that says

    The strchr() function returns a pointer to the LAST
    occurrence of the character c in the string s.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You're thinking of strrchr.

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

  9. #9
    Quote Originally Posted by paperbox005
    ok well im trying to do this..
    hi-there what-is-your-name
    and using strchr to check if there is a "-" in the string.
    so in this case it would point to the first "-" btw the "hi" and "there"
    the next time i call strchr() how is it possible for me to make it point to the "-" that is btw the "what" and "is" ? if that is even possible?
    Of course. Just start from the last position + 1.
    Code:
       char const s[] = hi-there what-is-your-name;
    
       char *p = strchr (s, '-');
    
       if (p != NULL)
       {
          /* first '-' found */
    
          strchr (p + 1, '-'); 
    
          /* etc. */
       }
    I'm quite sure you are going to find how to write some smart auto adaptative loop...
    Emmanuel Delahaye

    "C is a sharp tool"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strchr() function
    By sick in forum C Programming
    Replies: 13
    Last Post: 10-04-2008, 11:23 AM
  2. Parsing buffer with strchr
    By apsync in forum C Programming
    Replies: 3
    Last Post: 04-29-2007, 02:12 AM
  3. what is strchr func
    By vijlak in forum C Programming
    Replies: 2
    Last Post: 10-26-2006, 08:53 AM
  4. Using strchr() and moving the cursor up one row
    By Mavix in forum C Programming
    Replies: 7
    Last Post: 09-26-2006, 12:34 PM
  5. strchr
    By client in forum C Programming
    Replies: 2
    Last Post: 05-12-2002, 12:41 PM