STRINGS - case insensitive function

hi guys,

so im working on a function that gets two strings of chars.
The function returns the same number of characters that are in the same position in both strings. The function does not distinguish between lower and upper case letters.

my program is working. Only problem is that i cant find way to make the function case insensitive.

for example:

mama
MAjk

shout return 2.

so here's the code.
i want to figure out a way to do it without a built in system function.
thanks in advance!

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
30
31
  #include<iostream>
using namespace std;
int const SIZE = 20;
 
int sameLocation (char str1[], char str2[]) {
 
    int i, count = 0;
   
    for (i = 0; i < SIZE && str1[i] != NULL && str2[i] != NULL; i++) {
            if (str1[i] == str2[i]) {
                count++;
            }
        }
    return count;
}
 
 
int main() {
 
 
    char str1[SIZE], str2[SIZE];
 
    cout << "first string: ";
    cin.getline(str1, SIZE);
    cout << "second string: ";
    cin.getline(str2, SIZE);
 
    cout << sameLocation(str1, str2) << endl;
 
    system("pause");
}
you can't really. The usual way is to compare toupper or tolower of each character

eg
tolower(str1[i]) == tolower(str2[i])

be warned sometimes this gives odd results for Unicode. I think the issues are when the same letter visually is differently coded, but I am not an expert on the issues.
Last edited on
so you want to tell me there's no way so solve this?
this is a task from my school, so i believe there's a way! :(
Did you read what he said? Yes, of course, you can achieve almost anything in programming.
You just need to loop through each character and call tolower() on each character before you compare with ==.
jonnin already showed you the code.

Also, don't worry about unicode, it's certainly outside the scope of your assignment.
Last edited on
Possibly bad wording on my part.
C++ does not have a lot of the 'string processing language' stuff that many scripting languages have, such as a caseless compare done for you. You have to build it from what you do have.
Topic archived. No new replies allowed.