-2

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?

Gerald
  • 521
  • 1
  • 6
  • 16

1 Answers1

1

This is totally unrelated to upperCase, which is returning exactly the same size string you passed into it. The fix, change:

std::cin >> tweetString;

to

getline(std::cin, tweetString);
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720