I have no idea how to compare strings ignoring case!

I'm preparing myself for my Intro to C++ class that I will be taking in the fall, so right now I'm just self taught using a book I bought recently.

I'm trying to compare two strings ignoring case and using functions and a loop. My book wants me to use a loop to call the toupper function for every character.

The program has no syntactical errors, but I just took a shot in the dark at the comparison. My problem is that it returns false every time. Thanks in advance!

The entire program code so far (updated, and still returning false):

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <cctype>
#include <string>

using namespace std;

string upperCaseIt(string s);
bool sameString (string s1, string s2);

int main()
{
	string name1, name2;
	
	sameString(name1 , name2);

return(0);
}

string upperCaseIt(string s)
{
	for(int i = 0; i < s.length(); i++)
	{
		s[i] = toupper(s[i]);
	}
	return(s);
}

bool sameString (string s1, string s2)
{
	cout << "Enter Name 1:\t";
	cin >> s1;
	upperCaseIt(s1);
	cout << "\nEnter Name 2:\t";
	cin >> s2;
	upperCaseIt(s2);
		
		if(s1 == s2)
		{
			cout << "\nThe names are the same!\n";
			return true;
		}
		else
		{
			cout << "\nThe names are not the same!\n";
			return false; 
		}
}


But this is specifically the part I'm pretty sure is giving me the problem (updated):

1
2
3
4
5
6
7
8
string upperCaseIt(string s)
{
	for(int i = 0; i < s.length(); i++)
	{
		s[i] = toupper[i];
	}
	return(s);
}
Last edited on
Line 5: You're upshifting your index variable.

1
2
3
4
5
6
string upperCaseIt(string s)
{   for(int i = 0; i < s.length(); i++)
    {  s[i] = toupper(s[i]);
    }
    return s;
}

Sorry to sound extra noobish but what do you mean by "upshifting"?
upshift = to upper case
Oh I asked because I made the changes you suggested and it still is returning false, so I thought I was misunderstanding.

I've researched the heck out of this and everything seems right... but still false.
Last edited on
The issue is:

string upperCaseIt(string s)

This takes a copy of the string, alters the copy, then returns another copy of that copy. So, your name1 and name2 are never actually getting changed. With your usage, you'd want to change the method to:

void upperCaseIt(string &s)

This way it takes a reference of the string, then makes the changes.

Or, just use the return value of your current implementation of upperCaseIt (I use the return value as arguments in the changes I made).

I made some changes which should work (didn't actually test it). I made your code conform a little bit more to best practices.

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
32
33
#include <string>
#include <iostream>

std::string upperCaseIt(std::string s)
{
    for(int i = 0; i < s.length(); i++)
    {
        s[i] = toupper(s[i]);
    }
}

bool sameString(const std::string& s1, const std::string& s2)
{
    return (s1 == s2);
}

int main()
{
    std::string name1, name2;
    std::cout << "Enter both names, separated by a press of the 'enter' key." << std::endl;
    std::getline(std::cin, name1);
    std::getline(std::cin, name2);
    if(sameString(upperCaseIt(name1), upperCaseIt(name2)))
    {
        std::cout << "These are the same names!";
    }
    else
    {
        std::cout << "These are not the same names!";
    }

    return 0;
}
Last edited on
Line Line32, 35:
 
  upperCaseIt(s2);

upperCaseIt returns the modified string, but you're ignoring the return value.

Should be:
 
  s2 = upperCaseIt (s2);




Topic archived. No new replies allowed.