Feb 6, 2014 at 12:20am UTC
Can someone help me write a function that is called ignoreCaseCompare() that has two character (char) parameters. The function should return true if the two characters received represent the same letter, even if the case does not agree. Otherwise, the function should return false. Then, have a simple main() function that uses my ignoreCaseCompare(). Please any help will be considered helpful, thank you.
Feb 6, 2014 at 1:33am UTC
If you are allowed to use c/c++ libraries, a simple method will be:
1 2 3 4 5 6 7
#include <cctype> // c++
// #include <ctype.h> <-- c library
int ignoreCaseCompare(char a, char b)
{
return (tolower(a) == tolower(b))
}
There is also
strncasecmp if you are on a POSIX system or happen to have the
string.h library available to you:
http://linux.die.net/man/3/strncasecmp
If you are not allowed to use any external libraries, there is also bitwise versions of tolower/toupper which you can use instead:
1 2 3 4 5
// bitwise tolower
int tolower(char ch)
{
return ch | 0x20;
}
1 2 3 4 5
// bitwise toupper
int toupper(char ch)
{
return ch & 0x5F;
}
Last edited on Feb 6, 2014 at 1:36am UTC
Feb 18, 2014 at 2:27pm UTC
@mora15
This is simply a program where you enter two variables and then write a boolean statement. Try this code:
#include <iostream>
using namespace std;
bool ignoreCaseCompare (char, char);
int main ()
{
char first = ' ';
char second = ' ';
char answer = ' ';
cout << "Enter a letter: ";
cin >> first;
cout << "Enter a letter: ";
cin >> second;
cout << "";
answer = ignoreCaseCompare (first, second);
cout << endl;
return 0;
}
bool ignoreCaseCompare (char one, char two)
{
one = toupper(one);
two = toupper(two);
if (one == two)
cout << "The two letters are the same. ";
else
cout << "The two letters are not the same. ";
system("pause");
return 0; }
//end main function
Last edited on Feb 18, 2014 at 2:28pm UTC