How to check any received type (upper or lower case)?

Asked

Viewed 603 times

3

How can I make upper and lower case be ignored to compare with default word?

Ex: stack(default word to compare)

Words that could be received: "Stack" "Stack"

3 answers

3


Simple: convert text to uppercase and compare. Example code in C++:

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

bool compare(string s1, string s2)
{
    transform(s1.begin(), s1.end(), s1.begin(), ::toupper);
    transform(s2.begin(), s2.end(), s2.begin(), ::toupper);
    return s1 == s2;
}

int main() {

    string s[] = {"sTAck", "STACk", "STAcK", "SSTTAACCKK", "stack", "Extack"};

    for(int i = 0; i < 6; i++)
        cout << s[i] << (compare(s[i], "stack") ? " OK" : " NOK") << endl;

    return 0;
}

Upshot:

sTAck OK
STACk OK
STAcK OK
SSTTAACCKK NOK
stack OK
Extack NOK

See running in Ideone.

2

I don’t know much about C++, but if you don’t need to keep formatting the way it was typed you could use a method to turn letters into uppercase, if I’m not mistaken C++ uses the toupper(var) method to return a string to uppercase (if I’m not mistaken).

I’m more familiar with python, so I use the following:

a = "olamundo".upper() #para maiúsculas

or:

a = "OLAMUNDO".lower() #para minúsculas

I hope I’ve helped.

ps: you could store the method value in another variable and use the initial variable to keep formatting.

for reference: http://www.cplusplus.com/forum/beginner/75634/

1

There is no native case-insensitive string comparison function in C++.
On Windows you could use "stricmp" and Linux "strcasecmp".
In pure C++ it takes a little extra code.

Below, a way to make this comparison in pure C++ using the generic algorithm "Mismatch" and the function "toupper".

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

const string compare(const string& s1, string const& s2)
{
   if (s1.size() != s2.size()) return "diferentes";

   auto result = mismatch(s1.begin(), s1.end(), s2.begin(), s2.end(),
      [](char c1, char c2) { return ::toupper(c1) == ::toupper(c2); });

   return result.first == s1.end() ? "iguais" : "diferentes";
}

int main()
{
   vector<string> s { "sTAck", "STACk", "STAcK", "SSTTAACCKK", "stack", "xxx" };

   for (const auto& str : s)
       cout << str << ": " << compare(str, "stack") << endl;

   return 0;
}
  • Why the reference (&) in the cycle and what the need ?

  • 1

    Without the reference the variable "str" will be a copy of the vector element; with the reference the variable "str" will be the vector element itself. Whether it is necessary or not depends on the context. Whether the loop nay will change the vector element then the reference nay is necessary, but still the reference is used to avoid unnecessary copies.

  • Clarified. If the loop is in a function this copy would be deleted immediately after the function is executed correctly ?

  • The copy would exist only in the scope of the command "for".

Browser other questions tagged

You are not signed in. Login or sign up in order to post.