Reputation: 1
I wrote the code below to try the strstr()
function to search for a name in a list, but it is not finding it.
Can someone please explain why this code fails to find the name I am trying to look for?
#include <iostream>
#include <cctype>
#include <cstring>
using namespace std;
void toLowercase(char s[], int len);
int main()
{
const int names = 4; const int length = 30;
char studentnames[names][length] =
{
"Roka khalid",
"Ludi Fadi",
"Memo Fad"
};
char lookup[length];
char *strptr = nullptr;
int index;
cout << "\tstudent names\n\n";
cout << "enter the name you're looking for";
cin.getline(lookup, length);
toLowercase(lookup, strlen(lookup));
for (index = 0; index < names; index++)
{
strptr = strstr(studentnames[index], lookup);
if (strptr != nullptr)
break;
}
if (strptr != nullptr)
cout << studentnames[index] << endl;
else
cout << "no matching name was found";
}
void toLowercase(char s[], int len)
{
for (int i = 0; i < len; i++)
{
s[i] = tolower(s[i]);
}
}
//============
output: student names
//enter the name you're looking for Roka khalid
no matching name was found
Upvotes: 0
Views: 70
Reputation: 11
As already said in the comment(here) and also the Remy's answer, we need to takecare of Lower & Upper cases.
Moreover, if you are interested to know the minimal changes we need to do to make your existing code work, then see the below code.
for (index = 0; index < names; index++)
{
char lower_sname[length] = {};
strcpy(lower_sname, studentnames[index]); // TODO: Use strncpy()
toLowercase(lower_sname, strlen(lower_sname));**
strptr = strstr(lower_sname, lookup);
if (strptr != nullptr) {
break;
}
}
Upvotes: -1
Reputation: 598309
strstr()
performs a case-sensitive search. You are lowercasing the user's input, but you are not lowercasing the names in the list. "Roka khalid"
and "roka khalid"
are different strings from strstr()
's perspective, which is why you can't find it.
Try this instead:
char tmpName[length];
strcpy(tmpName, studentnames[index]);
toLowercase(tmpName, strlen(tmpName));
strptr = strstr(tmpName, lookup);
That being said, you are mixing C and C++ idioms. Choose one or the other. For example, using std::string
instead of C-style char[]
strings, and using C++ algorithms, your code could look something more like this:
#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>
using namespace std;
string toLowercase(string s);
int main()
{
const string studentnames[] =
{
"Roka khalid",
"Ludi Fadi",
"Memo Fad"
};
string input;
cout << "\tstudent names\n\n";
cout << "enter the name you're looking for:";
getline(cin, input);
auto found = find_if(begin(studentnames), end(studentnames),
[lookup = toLowercase(input)](const string &name){
return toLowercase(name).find(lookup) != string::npos;
}
);
if (found != end(studentnames))
cout << *found << endl;
else
cout << "no matching name was found";
}
string toLowercase(string s)
{
transform(s.begin(), s.end(), s.begin(),
[](unsigned ch){ return static_cast<char>(tolower(ch)); }
);
return s;
}
Upvotes: 1