Thread: good_strstr()

    #1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Lithuania
    Posts
    11
    Rep Power
    0

    good_strstr()


    Is there any way to make this function case-insensitive ?

    int good_strstr (char *str, char *key)
    {
    int cx, cl=0;

    for (cx=0;cx<strlen(str);cx++)
    {
    if (str[cx]==key[cl])
    {
    cl++;
    if (cl==strlen(key))
    return 1;
    continue;
    }
    cl=0;
    }
    return 0;
    }


    It searches for a key in string, if it founds - returns 1, else 0.


    Thanks.
  2. #2
  3. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    243

    I wrote this...


    You should be able to modify this to suite your needs (no warrantee expressed or implied!)...

    Code:
    /********************************
     *
     *
     *  strncmpIC, like <string.h>'s strncmp, but case free
     *  and returns 1 if a match is made, zero otherwise (opposite of strncmp)
     *
     *******************************/
    
    int strncmpIC(char * strA, char * strB, int maxlen){
        int i=0;
    
        while (strA[i] != '\0' && strB[i] != '\0' && i < maxlen){
            if (toupper(strA[i]) != toupper(strB[i]))
                break;
            i++;
        }
        if (i == maxlen)
            return 1;
        else
            return 0;
    }

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Lithuania
    Posts
    11
    Rep Power
    0
    yea, its good idea to use toupper()

    thanks

IMN logo majestic logo threadwatch logo seochat tools logo