Thread: case insensitive string comparison

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    3

    case insensitive string comparison

    Hi I'm doing a project for my C programming class and I can't figure out this one part of the assignment. The teacher changed it on us the original assignment was to filter through text messages we are given to look for certain words and prevent it from sending if it met certain criteria. One of those criteria is that there cannot be 3 mispelled words and recently the teacher ran a program and noticed that it said that a certain word was wrong because it wasn't capitalized or something. Anyway now we must check each word but it must be case insensitive the only way I know how to check is using the string compare function which is case sensitive is there anyone who can shoot me some ideas Thanks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Suppose that both the strings are in the same case. You can then use strcmp(), right? So, you can reduce the problem to one of ensuring that the strings are in the same case and using strcmp().
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    3
    Yes but the thing is that we are going to be given random messages so we will have to make sure that it is in fact case insensitive. I cant really make a function that makes sure that all the words are capitalized or something.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    I'm going to follow laserlights lead and not give you the answer...

    However I will provide you with a hint.

    You can't be sure of the capitalization of the message in it's original form. So you have to modify the message (or modify a copy of the message so the original isn't altered) so you KNOW what the case of the message is.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by osiris07
    I cant really make a function that makes sure that all the words are capitalized or something.
    Ah, but you can

    Check out the toupper() function from <ctype.h>
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    I guess he was going to give you the answer anyway ;P

  7. #7
    Registered User
    Join Date
    Apr 2009
    Posts
    3
    ahh so I could scan in the words and then use the toupper function not only on the text message but also on the dictionary words so that they should be exactly the same even if the dictionary says its lowercase or uppercase. Thanks guys I'll try that out.

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    You got it!

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    An easier (although less portable) solution is to use the stricmp() on Windows or strcasecmp() on Linux...
    Although for this assignment, it's probably better to do it yourself with toupper()...
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  10. #10
    Always learning
    Join Date
    Apr 2009
    Posts
    25
    I once took a quiz that asked me to write a case-insensitive version of strcmp. What I did was to simply compare every pair of characters (one from each string) after I had converted them to uppercase.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 03-05-2009, 11:32 AM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  3. Xmas competitions
    By Salem in forum Contests Board
    Replies: 88
    Last Post: 01-03-2004, 02:08 PM
  4. A simple array question
    By frenchfry164 in forum C++ Programming
    Replies: 7
    Last Post: 11-25-2001, 04:13 PM

Tags for this Thread