search for occurance of character in certain position in a string

Discussion in 'C Programming' started by magix, May 21, 2008.

  1. magix

    magix Guest

    How can I search for occurance of a character in certain position of a
    string
    I checked function strchr, but doesn't option to specify position.

    Thanks.

    Regards,
    Magix
     
    magix, May 21, 2008
    #1
    1. Advertising

  2. "magix" <> writes:
    > How can I search for occurance of a character in certain position of a
    > string
    > I checked function strchr, but doesn't option to specify position.


    If you're checking a certain position, it's not really a search, is it?

    It sounds like you're looking for something that will tell you, for
    example, whether or not the character 'C' occurs in the third position
    (position 2) in the string "ABCDE", regardless of whether it also
    occurs anywhere else. If so, all you need is a single comparison. If
    not, please clarify what you're asking for.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, May 21, 2008
    #2
    1. Advertising

  3. "magix" <> wrote:
    > How can I search for occurance of a character
    > in certain position of a string


    if (strlen(s) > n && s[n] == ch)
    /* yes, char ch at position n in s */

    --
    Peter
     
    Peter Nilsson, May 21, 2008
    #3
  4. magix

    user923005 Guest

    On May 20, 7:29 pm, "magix" <> wrote:
    > How can I search for occurance of a character in certain position of a
    > string
    > I checked function strchr, but doesn't option to specify position.

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>

    long location(char *string, int ch)
    {
    char *where = strchr(string, ch);
    long index = -1;
    if (where) {
    index = where - string;
    }
    return index;
    }
    int main(void)
    {
    char string[] = "Hello, my name is skinner the
    grinner! What's yours?";
    long loc;
    loc = location(string, '!');
    if (loc >= 0) {
    printf("position is %ld\n", loc);
    putchar(string[loc]);
    } else {
    puts("Not found.");
    }
    return 0;
    }
     
    user923005, May 21, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. news.hku.hk
    Replies:
    7
    Views:
    7,678
    Michiel Salters
    Apr 26, 2004
  2. Patrick Spence
    Replies:
    2
    Views:
    120
    Patrick Spence
    Aug 19, 2006
  3. Replies:
    2
    Views:
    80
  4. Stephen O'D

    Matching final occurance of a string in text

    Stephen O'D, Jun 1, 2005, in forum: Perl Misc
    Replies:
    6
    Views:
    84
    Anno Siegel
    Jun 2, 2005
  5. Question Boy
    Replies:
    12
    Views:
    223
    Michael Wojcik
    Sep 4, 2009
Loading...

Share This Page