locked
case sensitivity in C++ RRS feed

  • Question

  • hi.

    i am currently working on C++ language.

    here i am stuck up with a little difficulty.

    since C++ is a case sensitive language and when we ask a user to enter certain thing, being a lay man he wont be knowing about the case sensitivity.

    is there any function to make the compiler consider lower case letters as upper case and vice versa??

    i want to know this function in C++.

    Saturday, May 14, 2011 3:21 AM

Answers

  • ...then it makes sense to take the string that user has typed in and change it to either all upper case or all lower case before making the comparison. The other posts explain how.
    • Marked as answer by lucy-liu Monday, May 23, 2011 3:24 AM
    Saturday, May 14, 2011 4:11 AM

All replies

  • You can either convert each character to lower case or upper case as they are entered.

    To convert to lower case add 32 to the value of the upper case character -

    if (C >= 'A' && C <= 'Z') C += 32;

    Do the reverse to convert to upper case -

    if (C >= 'a' && C <= 'z') C -= 32;

    The above examples assume C as a single character variable.

     

    Using C++ you can convert a string to upper case like so -

    std::transform(str.begin(), str.end(), str.begin(), ::toupper);

    Here is to convert to lower case -

    std::transform(str.begin(), str.end(), str.begin(), ::tolower);

    If you only want to compare strings without considering case instead of actually converting them, there are several functions available for this.
    _stricmp
    CStringT::CompareNoCase

     


    «_Superman_»
    Microsoft MVP (Visual C++)

    Polymorphism in C
    Saturday, May 14, 2011 3:32 AM
  • The case sensitive aspect of C++ has nothing to do with user input. The compiler only processes programmer input, not user input.

    You may be looking for the toupper and tolower library functions.

    Saturday, May 14, 2011 3:34 AM
  • actually the thing is in my program i prompted the user to input a name of song. now i want to do that whether the user writes for example "dreams" or "Dreams" the complier should be able to recognize that song.
    Saturday, May 14, 2011 3:44 AM
  • ...then it makes sense to take the string that user has typed in and change it to either all upper case or all lower case before making the comparison. The other posts explain how.
    • Marked as answer by lucy-liu Monday, May 23, 2011 3:24 AM
    Saturday, May 14, 2011 4:11 AM
  • oh thank you so much.
    Saturday, May 14, 2011 5:13 AM