0

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(); 
}
IntoTheDeep
  • 4,027
  • 15
  • 39
  • 84
  • 1
    Possible duplicate of [Case insensitive std::string.find()](http://stackoverflow.com/questions/3152241/case-insensitive-stdstring-find) – mligor Apr 09 '17 at 21:14
  • What language? There are languages where one lower/upper case letter corresponds to multiple possibilities, and in some cases multiple characters at once. – Yakk - Adam Nevraumont Apr 10 '17 at 01:46

5 Answers5

3

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

Penguino
  • 2,136
  • 1
  • 14
  • 21
2

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

Community
  • 1
  • 1
Kyle Johnston
  • 41
  • 1
  • 6
2

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

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

Community
  • 1
  • 1
mligor
  • 81
  • 6
0

Make all the letters in the string capital/small.

0

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