Thread: strcmp, a problem with capital letters

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    7

    strcmp, a problem with capital letters

    Hey all,

    I'm basically in the process of sorting words in a text file. As it currently stands everything is working, however it seems that words beginning with a capital letter are placed before lowercase letters, regardless of their position in the alphabet (I'm guessing this is because of the byte size).

    Was just wondering if there were any easy solutions available? I'm guessing the easiest alternative would be to make all the words lower or upper cases, but after searching google and trying strlwr and tolower, neither of the functions worked

    Any help welcome,
    Thanks.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You might have a non-portable stricmp() or strcasecmp() or something along those lines.

    Or you have to write your own equivalent, using say tolower() on each char you compare.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    if everything was working then you wouldnt have a problem right?

    are you only using 'strcmp' to compare the strings? do you realize that: 'A' < 'a'? refer to the ASCII table for more info on that.

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    7
    Quote Originally Posted by Salem View Post
    You might have a non-portable stricmp() or strcasecmp() or something along those lines.
    hmm, I tried changing strcmp() to strcasecmp() and got the same output.
    Quote Originally Posted by Salen View Post
    Or you have to write your own equivalent, using say tolower() on each char you compare.
    I attempted to make my own but didn't get very far, heh. The tolower() I referred to was something I found whilst searching google.

    Quote Originally Posted by nadroj
    if everything was working then you wouldnt have a problem right?
    True
    Quote Originally Posted by nadroj
    are you only using 'strcmp' to compare the strings? do you realize that: 'A' < 'a'? refer to the ASCII table for more info on that.
    I'm currently using strcmp() yes. I'm aware that 'A' < 'a' with regards to binary, but was under the impression that strcmp() would compensate for the correction in general English (e.g 'B' > 'a').

    Is there a function that can permanently convert a string to lower case?

    -Update
    Apologies Salem, it seems that I hadn't fully changed my strcmp() to strcasecmp(). The program is now 'working as intended'.
    Many thanks
    Last edited by RichardH; 04-22-2007 at 01:07 PM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    No, but as Salem mentioned you can use tolower(), which works on characters, to write a function to convert strings to lowercase.
    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
    Mar 2007
    Posts
    7
    Quote Originally Posted by laserlight View Post
    No, but as Salem mentioned you can use tolower(), which works on characters, to write a function to convert strings to lowercase.
    ah, good to know for future reference, thanks

  7. #7
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    I'm aware that 'A' < 'a' with regards to binary, but was under the impression that strcmp() would compensate for the correction in general English (e.g 'B' > 'a').
    strcmp() does a lexigraphical comparison, not an alphabetical comparison. If you want it to compensate, you have to write your own function that does it.
    Is there a function that can permanently convert a string to lower case?
    Yes, you can check your compiler for _strlwr or something like that. Most compilers will have a comparable function that converts a string to lower case. But according to people here, you have to write your own portable function because anything nonportable is evil and anyone who suggests using something nonportable is stupid. At least this time it's easy:
    Code:
    char *strLower( char *str ) {
      char *temp;
    
      for ( temp = str; *temp; temp++ ) {
        *temp = tolower( *temp );
      }
    
      return str;
    }

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    But according to people here, you have to write your own portable function because anything nonportable is evil and anyone who suggests using something nonportable is stupid.
    Well, you could wrap the unportable function such that it can be easily switched for something else when ported elsewhere.
    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

  9. #9
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    I could, but I get jumped on even if I write something portable that someone doesn't like. So I'll just hedge my bets and hope my style isn't nitpicked.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You're just being whiny. The reason I try to use portable code is because then we don't have to worry about someone coming along later and saying "This doesn't work for me!", hopefully avoiding hundreds of threads on the exact same topic and problem.


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

  11. #11
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    You're just being whiny.
    Yeah, I know. The way I see it, all I can do is have fun with it. You guys expect me to be perfect according to your standards, but your standards are impossible to predict and it's impossible to be perfect. So if I'm going to get flamed in public and in private no matter what I do, I'll at least enjoy myself.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I don't expect you to be anything. You worry too much. The fact is I just don't give a damn how you are. If I feel like commenting on something, I do. If I don't, I don't. But now we're way off track of this thread.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 11-16-2006, 01:43 PM
  2. Capital Letters ...
    By twomers in forum C++ Programming
    Replies: 11
    Last Post: 01-11-2006, 03:10 AM
  3. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  4. Another assignment problem
    By the_winky_files in forum C Programming
    Replies: 14
    Last Post: 10-03-2005, 10:33 AM
  5. check for capital letters
    By blackwyvern in forum C++ Programming
    Replies: 3
    Last Post: 01-30-2002, 10:32 PM