Strings

I made a simple program to compare two string values using If statement, in functions, Please any1 guide me or sort it out for me that when the program ask the user to enter the input e.g Enter first Name and then Enter second name, so if i give both names in lower case so they match and the result is true , but when i give the same names as input but one input/name in upper case and the other in lower case, then it says names didn,t matched, so whats the method that the compiler dont care about whether I entered lower case or upper case letter,.. And when I enter junaid as a first input and then JUNAID as second input so it displayed that Names Matched...
Below is the code

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

using namespace std;
void compare(string a, string b); //Function Prototyp//

int main()
{
    string x,y,z;
    cout<<"Enter First Name : " <<endl;
    cin >> x;
    cout<<"Enter Second Name : " <<endl;
    cin >> y;

    compare(x,y);
    return 0;
}
void compare(string a, string b){
  if(a==b){
    cout<<"Name Matched" << endl;
    
  }
  else
    cout<<"Name didn,t Match" <<endl;

}
Last edited on
closed account (3qX21hU5)
Remember uppercase and lowercase matter when checking if two strings are equal or not.

One way you could go about it turn whatever the user enters into all lowercase with tolower() http://www.cplusplus.com/reference/cctype/tolower/?kw=tolower that way you don't have to worry about if they enter any upcase letters.

Just iterate through each string and turn all upcase letters to lowercase letters.
Last edited on
Hey Zereo I didn,t get you, if you kindly read my problem above and then give me the solution accordingly so i shall be very thankful....
Hey I think this is what you are looking for

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
#include <iostream>
#include <string>
using namespace std;

bool bCompare(string, string);

int main()
{
    string x, y, z;

    cout << "Enter First Name: ";
    cin >> x;

    cout << endl << "Enter Second Name: ";
    cin >> y;

    if (bCompare(x, y))
        cout << endl << "Names matched!";
    else
        cout << endl << "Names didn't match.";

    return 0;
}

bool bCompare(string a, string b)
{
    if (sizeof(a) == sizeof(b))
    {
        for(int iii = sizeof(a) + 1; iii > 0 ; --iii)
            a[iii - 1] = tolower(a[iii - 1]);

        for(int iii = sizeof(b) + 1; iii > 0 ; --iii)
            b[iii - 1] = tolower(b[iii - 1]);
    }
    if (a == b)
        return true;
    else
        return false;
}


I used sizeof() which returns the value of the number of characters. Then tolower() converts each letter into a lowercase then you just compare them.
closed account (3qX21hU5)
I'm sorry but we aren't your employees and we won't just give you the code solution for your problems. I have no problem helping people that show they want to learn and do things their selves, but I do have a problem helping people that show no effort and want others to do their work for them.

I gave you the resources you need so a take a few minutes to get familiar with them.

If you have any questions I would be more then happen to help walk you through anything you don't understand but I won't as you say "Read your problem above and give you the solution".
Topic archived. No new replies allowed.