IntoTheDeep
IntoTheDeep

Reputation: 4118

c++ small capital letter no difference

I have method that find occurrences of string in other string. My question is how to make it to do no difference between small and capital letter?

int occurrences = 0;
string::size_type start = 0;

while ((start = base_string.find(to_find_occurrences_of, start)) != string::npos) {
    ++occurrences;
    start += to_find_occurrences_of.length(); 
}

Upvotes: 0

Views: 2106

Answers (5)

mligor
mligor

Reputation: 81

Check this answer - using std::search with a custom predicate seams the best way for me

https://stackoverflow.com/a/3152296/6910287

Upvotes: 2

Kyle Johnston
Kyle Johnston

Reputation: 41

Normalize both strings to upper or lowercase before comparison.

An explanation of how to do this using the standard library's tolower() or toupper() functions with the transform() function is given here: https://stackoverflow.com/a/313990/2355444

Upvotes: 2

Try using the tolower() or toupper() functions from <cctype> (ctype.h) libraries on both sides of the comparison so that any difference becomes negligible.

Upvotes: 0

Rick de Gier
Rick de Gier

Reputation: 62

Make all the letters in the string capital/small.

Upvotes: 0

Penguino
Penguino

Reputation: 2176

Why not just set both strings to uppercase before you do the search?

Upvotes: 3

Related Questions