String accept both upper and lowercase?

Hello guys,

i have 2 question

1st: Is it possible to accept both upper and lowercase?
For example you have this code

1
2
3
4
5
6
7
8
9
  string question;
  
  cout << "sda " << endl;
  cin >> question
  
  if(question == "yes" || question == "YES")
  {
      //code
   }


and i want to do this

1
2
3
4
5
6
7
8
9
  string question;
  
  cout << "sda " << endl;
  cin >> question
  
  if(question == "yes")
  {
      //code
   }

but it should accept upper and lowercase letters.

2nd. How can i set a string to not allow spaces?
You can see ASCII table letters are just numbers for the computer.For example you can get a number from a letter
5='0'-'5';
And answer for your question is yes,it is possible
You cann create class String that has string in it and if the user inputs space in string ,you can make it throw exeption or to just connect characters without spaces.
can you explain with an example how to not allow spaces? thanks in advance
When you read a string with >> it normally ignores spaces. For a case-insensitive compare, you can do something like this. Test it with yes, YES, yEs, etc. Also try with spaces in front and/or after (which the >> should ignore).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <string>
#include <cctype>  // toupper

bool equal(const std::string& a, const std::string& b)
{
    if (a.size() != b.size())
        return false;
    for (size_t i = 0; i < a.size(); ++i)
        if (std::toupper(a[i]) != std::toupper(b[i]))
            return false;
    return true;
}

int main()
{
    while (true)
    {
        std::string answer;
        std::cout << "What? ";
        std::cin >> answer;
        if (equal(answer, "quit"))
            break;
        if (equal(answer, "yes"))
            std::cout << "is equal\n";
        else
            std::cout << "not equal\n";
    }
}

Last edited on
another way is to pre-process the input, if that is acceptable:

string s;
cin >> s;
s.erase(std::remove(s.begin(), s.end(), ' '), s.end()); //removes ALL spaces from the string.
//you don't really need to remove spaces with cin >> but you may if you choose to use getline.
//uppercase the string:
std::transform(s.begin(), s.end(),s.begin(), ::toupper);
now you can compare it or whatever else:
if(s == "YES"s) //the s is required to use == ( s is NOT the variable name, in hindsight string s was poor for the example, here s is just making the constant text a string)
{
}
Last edited on
You may also want to consider using a single character instead of the string.

@jonin, That's some nifty usage of <algorithm>. But you don't need the string literal operator on "YES" if one of the operands is a std::string. Also, it should be mentioned that using ""s requires C++14 and using namespace std::string_literals.
Last edited on
Good point. Ive been dealing with some mixed c-string code and may be over-using the 's' trick due to that.
dutch wrote:
Also, it should be mentioned that using ""s requires C++14 and using namespace std::string_literals.

Or using namespace std;
Last edited on
@Peter87, Indeed. But it's good to know the other if you don't want to dump everything.

@jonnin, Do you know why it doesn't work if you put std::toupper instead of ::toupper in the std::transform?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>
#include <algorithm>
//#include <cctype>  // apparently not needed; is that standard?

int main() {
    std::cout << "yo: ";
    std::string s;
    std::getline(std::cin, s);

    s.erase(std::remove(s.begin(), s.end(), ' '), s.end());
    std::transform(s.begin(), s.end(), s.begin(), /*std*/::toupper);

    if (s == "YES")
        std::cout << "yep\n";
    else
        std::cout << "nope\n";
}

Last edited on
I think maybe it's because there is another overload of toupper() in header <locale> (which gets pulled in by one of the other headers).
it seems likely mine was using the C version, actually...
looking at it now.
the cctype one is failing due to template syntax. So its not using that one.
and its not using standard one.
and locale one has an extra parameter, so its not using that one.
so I am nearly convinced its using the C function.

to use std:: you may have to wrap it in a lambda, that should work.
Last edited on
Topic archived. No new replies allowed.