function similar to strpos() ?

This is a discussion on function similar to strpos() ? within the C Programming forums, part of the General Programming Boards category; hey, i was wondering if there was a function similar to strpos() in PHP to test if certain characters exist ...

  1. #1
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378

    function similar to strpos() ?

    hey, i was wondering if there was a function similar to strpos() in PHP to test if certain characters exist within a string? i say similar because i'm looking for something that will test a string for a set of characters, but the characters wont necessarily be next to each other.


    or the way i thought about doing it, was to add all the possibilities to a multi-dimensional array and then strcmp()'ing each element with the test string in a loop..only problem is, i dont know how to access the members of the array, lol. but lets say theres 5 possibilities and out of those 5, i want to test if 4 of them are a combination of 1,2,3,4 2,3,4,5 or 3,4,5,6. the best way to accomplish this would be.... ?

    thanks
    Last edited by willc0de4food; 10-06-2005 at 09:08 PM.
    Registered Linux User #380033. Be counted: http://counter.li.org

  2. #2
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Use strchr for finding a single character, and strstr for finding a string within a string.
    Last edited by cwr; 10-06-2005 at 09:11 PM.

  3. #3
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    so what would be better than..
    Code:
    if (strchr(numbers, 50) != NULL)
    {
        if (strchr(numbers, 51) != NULL)
        {
    	 if (strchr(numbers, 52) != NULL)
    	 {
    	 ....
    ? lol
    i'm looking for fast / small / efficient but i'm willing to code whatever i need.




    mm..thats a good site.
    i'm thinking strpbrk would work a lil better.. ?
    Registered Linux User #380033. Be counted: http://counter.li.org

  4. #4
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    See my edit above, strstr to find a string of characters that are next to each other. If they might not be next to each other, you will have to use multiple strchr's, or you could iterate through the array, and flag when you've found the right characters. It depends on exactly what you are trying to search for. By the way, what is 50, 51, 52 above? If they are the ASCII values for numbers, use '2', '3', '4', instead. It's more readable, and doesn't depend on your platform being ASCII.

    Edit: sure, strpbrk will allow you to find any number of characters, but it only returns the first occurence of one of them. You would still have to call it over and over.

  5. #5
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    50, 51, 52 are 1, 2, 3..i have problems with other ways of comparing char's, lol
    i'm thinkin i'll have to call anything multiple times lol
    i could even do strcmp in a loop, but i wouldn't know how to go about comparing strings from a multi-dim array. do you know?


    what do you mean by "iterating through the array"? thanks
    Registered Linux User #380033. Be counted: http://counter.li.org

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Code:
    char arr[10] = { /* characters here */ };
    for(i=0; i<i; i++)
      /* do something with arr[i] here */
    Also its better to use '1', '2', '3', ... instead of their ascii values

  7. #7
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    You should use '1' instead of 49. Must be single quotes. You won't have an issue. C looks at '1' and translates it to the integer value anyway.

    To compare strings from a multi dimensional array, go through each of them in a for loop.

    Iterating through the array, as in, going through the string character by character, comparing and flagging when you find the characters you are interested.

    Try to explain what exactly you want to achieve, then I can suggest elegant solutions.

    Quote Originally Posted by Thantos
    Also its better to use '1', '2', '3', ... instead of their ascii values
    As I already pointed out in a prior post.

  8. #8
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    the item i want isn't in the multi-dim array. those are the possibilities of strings i'm looking for.
    what i have is a set of 5 randomly generated numbers. then i have them stored in a char variable. its a little game, and depending on what numbers they are - the user can put them different places. basically like a "small straight" for yahtzee. you only need 4 numbers to get a small straight, but either way you still have 5 die. now you can look at the die and say "yep, theres 4 consecutive numbers" but i need my program to basically do that

    thanks
    Registered Linux User #380033. Be counted: http://counter.li.org

  9. #9
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    So you're actually looking for consecutive numbers, not searching for particular numbers. If that's the case, you are realling just wanting to check if the digit before is equal to one less than the current digit, and that that occurs 3 times in a row:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    int consec(char *);
    
    int main()
    {
        char a[][6] = { "52915", "12394", "34560", "12345", "82345", "23459", "67923", "12356", "43210", "90125" };
        int i;
    
        for (i = 0; i < sizeof(a)/sizeof(a[0]); i++)
            printf("%s: %d\n", a[i], consec(a[i]));
    
        return 0;
    }
    
    int consec(char *s)
    {
        int i;
        int conseccount = 0;
    
        if (!s || strlen(s) < 4)
            return -1; /* invalid input */
    
        for (i = 1; i < strlen(s); i++)
        {
            if (!isdigit(s[i]))
                return -1; /* invalid input */
            if (s[i-1] == s[i]-1)
            {
                conseccount++;
                if (conseccount == 3)
                    return 1;
            }
            else
                conseccount = 0;
        }
        return 0;
    }
    Output:
    Code:
    52915: 0
    12394: 0
    34560: 1
    12345: 1
    82345: 1
    23459: 1
    67923: 0
    12356: 0
    43210: 0
    90125: 0
    Of course, you could trivially make consec() generic to check for n consecutive numbers, or report the maximum consecutive run it finds.
    Last edited by cwr; 10-07-2005 at 01:33 AM.

  10. #10
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    well no, because with my "small straight" example, you could have "12234" and you'd still have 4 consecutive numbers, they just dont exist next to each other (thanks to a sorting function )..and like in your sample, "12394" that still has the numbers 1-4, theres just a 9 in there.. lol...maybe modify the sorting function so that if theres 2 numbers that are the same, throw the 2nd at the end..? mm

    thanks



    p.s. sorting function:
    Code:
        do {
             for (i = 0; i < 6; i++)
             {
                if (num[i] > num[i+1])
                {
                    temp = num[i+1];
                    num[i+1] = num[i];
                    num[i] = temp;
                }
             }
             done = done + 1;
        } while (done < 6);
    Last edited by willc0de4food; 10-08-2005 at 04:37 AM.
    Registered Linux User #380033. Be counted: http://counter.li.org

  11. #11
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Why not just skip if the number is the same, like this:
    Code:
    int consec(char *s)
    {
            int i;
            int conseccount = 0;
    
            if (!s || !strlen(s))
                    return 0;
    
            for (i = 1; i < strlen(s); i++)
            {
                    if (!isdigit(s[i]))
                            return -1; /* invalid input */
                    if (s[i-1] == s[i]-1)
                    {
                            conseccount++;
                            if (conseccount == 3)
                                    return 1;
                    }
                    else if (s[i-1] == s[i])
                            continue;
                    else
                            conseccount = 0;
            }
            return 0;
    }
    Last edited by cwr; 10-08-2005 at 04:43 AM. Reason: Formatting

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by willc0de4food
    hey, i was wondering if there was a function similar to strpos() in PHP to test if certain characters exist within a string? i say similar because i'm looking for something that will test a string for a set of characters, but the characters wont necessarily be next to each other.
    From the description above, why not just do:
    Code:
    int hasset( char *str, char *set )
    {
        int x = 0;
        for( ; *set; set++ )
            x += strchr( str, *set ) ? 1 : 0;
        return x == strlen( set );
    }
    This will return true or false, if every character in 'set' is found in 'str'.


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

  13. #13
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    B-E-A-UTIFUL


    thanks guys. i've got cwr's function working..i'll have to try out yours quzah.
    Registered Linux User #380033. Be counted: http://counter.li.org

  14. #14
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    willc0de4food,

    No problem. In future, try to state the exact nature of the problem in the first place, possibly offering solutions you've thought of. This one took a while to solve because you first asked how to do a certain thing, but that turned out not to be what you wanted, I think...

  15. #15
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    cept sometimes, (i believe this happens in my sort function) like.. if your numbers are 23456 then it replies to you saying your numbers are 23444. sometimes, when the numbers are lower it will print up to 4 and then just print 4's. any ideas why?

    mm...i'm falling asleep typing. night
    Registered Linux User #380033. Be counted: http://counter.li.org

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strpos() function in c?
    By mikewagan in forum C Programming
    Replies: 3
    Last Post: 03-19-2008, 06:05 AM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  4. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 07:55 AM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM

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