Thread: Find word with uppercase and lowercase letters

  1. #1
    carpe diem
    Join Date
    Jan 2010
    Posts
    46

    Find word with uppercase and lowercase letters

    Is there a function I can use that will find the variation of a determined word with any combination uppercase/ lowercase letters?

    eg: If I look for the word "rat" i also want to find the variations: RAT, Rat, RAt, raT, RaT and rAt

    I am currently using strstr but it only returns TRUE when the word found is in the exact same format as the word it's looking for.

  2. #2
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    before you strcmp() the words, run them all through tolower()

  3. #3
    carpe diem
    Join Date
    Jan 2010
    Posts
    46
    yup that works... thanks=)

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by doia View Post
    yup that works... thanks=)
    How's it going to work when one of the strings is a string constant, which isn't writable? What if you don't want to ruin the string by changing its case?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    You may have stricmp().

  6. #6
    carpe diem
    Join Date
    Jan 2010
    Posts
    46
    @brewbuck: fortunately my strings are not constant
    @nonoob: YES I do, exactly what I wanted! merci

    now only one more thing for my code. I also want to find abbreviations:

    for example if I look for "Vertical Stability" I want to find all instances of "vert. stability","vert stab", "vertical stblity". I guess it'd be something like: if a certain percentage of the letters match then I want it to return true, I really have no idea how to implement that. any help is appreciated!
    Last edited by doia; 07-15-2010 at 03:41 PM.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by doia View Post
    @brewbuck: fortunately my strings are not constant
    I bet brewbuck intended his statement "just in case" and "for future extending". You may simply convert the string to lower case and it may work for now, but it may not work in tthe future. Stricmp would. But I advise you to write on yourself first...
    In my opinion, you should only use functions that you can write yourself, so you understand them thoroughly. (Okay, honestly, I couldn't write printf because I don't know all details of format strings, but I know enough to write them to support anything I ever use in it).

  8. #8
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Then you'll have to write your own "stricmp" function... starting after every space to compare the word with your library of words... but counting how many letters match. Then picking the best match.

  9. #9
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    Quote Originally Posted by brewbuck View Post
    How's it going to work when one of the strings is a string constant, which isn't writable? What if you don't want to ruin the string by changing its case?
    Could copy them into a temp variable before you compare.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It doesn't matter if they're constant or not. You can still run tolower on them as you compare each letter.
    Code:
    while( *s1 && *s2 && tolower( *s1++ ) == tolower( *s2++ ) );
    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Counting uppercase and lowercase letters in a text
    By Tintu in forum C Programming
    Replies: 2
    Last Post: 02-06-2008, 10:15 PM
  2. how to detect a lowercase or uppercase char?
    By Axel in forum C Programming
    Replies: 5
    Last Post: 09-04-2005, 12:28 PM
  3. Uppercase and lowercase
    By StarOrbs in forum C++ Programming
    Replies: 4
    Last Post: 03-09-2005, 04:18 PM
  4. Replies: 14
    Last Post: 06-06-2004, 03:51 PM
  5. C++ newbie..convert string from lowercase to uppercase
    By wireless in forum C++ Programming
    Replies: 5
    Last Post: 02-25-2002, 08:49 PM