search for occurance of character in certain position in a string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • magix

    search for occurance of character in certain position in a string

    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


  • Keith Thompson

    #2
    Re: search for occurance of character in certain position in a string

    "magix" <magix@asia.com 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_Keit h) kst-u@mib.org <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"

    Comment

    • Peter Nilsson

      #3
      Re: search for occurance of character in certain position in a string

      "magix" <ma...@asia.com 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

      Comment

      • user923005

        #4
        Re: search for occurance of character in certain position in a string

        On May 20, 7:29 pm, "magix" <ma...@asia.com 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("positio n is %ld\n", loc);
        putchar(string[loc]);
        } else {
        puts("Not found.");
        }
        return 0;
        }

        Comment

        Working...