Comparing strings regardless of case

testing strings to see if they contain certain combos. Instructions were to check last 2 characters to see if word ended in es and check substring to see if began yan....just remembered its supposed to be case insensitive...there isn't ignoreCase in c++ right? help?


/*Joseph Paigo
CISS 290
ex 3-10
This will compare strings
two ways, by charater and
by substring*/
#include<iostream>
#include<string>
using namespace std;
void main()

{
int i, j,k;
string str[3];

for (i = 0; i < 3; i++)
{
cout << "Enter a string: " << endl;

getline(cin, str[i], '\n' );

}

for (j = 0; j < 3; j++)
{
k = str[j].length();

if (str[j][k - 1] == 's' && str[j][k - 2] == 'e')
cout << str[j] <<"\n"<< endl;
if (str[j].substr(0, 3) == "yan")
cout << str[j]<<"\n"<<endl;
}
}
NOTE: for whatever reason he wants void main

Sadly, there isn't really a built-in way to do case-insensitive string compare. At least not one that I know of.

I suggest writing your own function, e.g. "iequals(a, b)" and convert each character to lower before comparing.

Copied from SO cuz I'm lazy
https://stackoverflow.com/a/4119881
1
2
3
4
5
6
7
8
bool iequals(const string& a, const string& b)
{
    return std::equal(a.begin(), a.end(),
                      b.begin(), b.end(),
                      [](char a, char b) {
                          return tolower(a) == tolower(b);
                      });
}


If you can't do lambdas, then try writing it yourself with a for loop.
Last edited on
this may depend a bit on your data. if you suspect most of the time the strings match exactly, you might want to do a normal compare and if that fails, then check caseless as above. this does more work when they are not exact, and less work when they are, so it may or may not help.
Another option is to create your own char_traits, though it looks a bit complicated.
https://www.linuxtopia.org/online_books/programming_books/c++_practical_programming/c++_practical_programming_067.html
> there isn't ignoreCase in c++ right?

Usually, the regular expressions library is used for pattern matching of text.
It supports matching of characters ignoring case.
std::regex_constants::icase https://en.cppreference.com/w/cpp/regex/syntax_option_type

For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>
#include <regex>

int main()
{
    std::string str ;
    std::cout << "enter a string: " ;
    std::getline( std::cin, str ) ;
    std::cout << str << '\n' ;

    // yan.* : 'yan' folowed by zero or more (any) characters
    // std::regex_constants::icase - ignore case
    const std::regex begins_with_yan( "yan.*", std::regex_constants::icase ) ;
    if( std::regex_match( str, begins_with_yan ) ) std::cout << "\tstring begins with 'yan'\n" ;

    // .*es : zero or more (any) characters folllowed by 'es'
    const std::regex ends_in_es( ".*es", std::regex_constants::icase ) ;
    if( std::regex_match( str, ends_in_es ) ) std::cout << "\tstring ends in 'es'\n" ;
}
Topic archived. No new replies allowed.