I'm doing a challenge on HackerRank which involves finding the number of times a substring appears in a series of strings. The user inputs how many strings they would like to test to see if they contain "hackerrank", insensitive to case. I've pulled a method from another popular answer on here to convert a string to uppercase. Here is the code:
#include <iostream>
#include <string>
#include <cstdio>
#include <cctype>
std::string upperCase(std::string input) {
for (std::string::iterator it = input.begin(); it != input.end(); ++ it)
*it = toupper(*it);
return input;
}
int main(){
int numofStrings;
std::cin >> numofStrings;
std::string tweetString;
int hackerRankOccurrences = 0;
for (numofStrings; numofStrings>0; numofStrings--) {
std::cin >> tweetString;
bool found = upperCase(tweetString).find("HACKERRANK")!=std::string::npos;
if (found) {
hackerRankOccurrences++;
}
}
std::cout << hackerRankOccurrences << std::endl;
}
The problem, in my understanding, is that this upperCase method returns every word of the input on a different line, e.g.:
Hello, this is a sentence
becomes:
HELLO,
THIS
IS
A
SENTENCE
This causes the for loop to iterate through every word in the string, decrementing the total user input numofStrings for every word used.
How can I get upperCase to return a single string / single line of output?