You are to write two functions, printString() and testString(), which are called from the main function.
printString (string)prints characters to std::cout with a space after every character and a newline at the end.
testString (string)returns true if the stringcontains two consecutive characters that are the same, false otherwise. See the main() to see how the two functions are called.
Some sample runs are given below:
- string: “hello”
- printString prints: h e l l o
- testString returns: true (because of ‘l’ and ‘l’)
- string: “world”
- printString prints: w o r l d
- testString returns: false
- string: “hello world”
- printString prints: h e l l o w o r l d
- testString returns: true (because of ‘l’ and ‘l’)
Note:
- Write only the two functions in the space provided.
- You can change the main function for your own testing. Your code will be tested with a similar main function.
Hints:
- You can access thei-th character of a string s with s[i]. You can compare characters using the == operator.
- The number of characters in the string is s.size().
prob1.cpp
#include
#include
using std::string;
using std::cout;
using std::endl;
bool checkAnswer(const string &nameOfTest, bool received, bool expected);
// Implement printString here
// Implement testString here
// EDIT CODE BELOW ONLY FOR TESTING (ANY CODE BELOW WILL BE OVER-WRITTEN DURING GRADING WITH DIFFERENT TESTS)
int main() {
cout
{
string s = "hello";
cout
printString(s);
checkAnswer(s, testString(s), true);
}
{
string s = "world";
cout
printString(s);
checkAnswer(s, testString(s), false);
}
{
string s = "hello world";
cout
printString(s);
checkAnswer(s, testString(s), true);
}
{
string s = "zzz";
cout
printString(s);
checkAnswer(s, testString(s), true);
}
{
string s = "snakess";
cout
printString(s);
checkAnswer(s, testString(s), true);
}
{
string s = "a";
cout
printString(s);
checkAnswer(s, testString(s), false);
}
// system("pause"); // comment/uncomment if needed
return 0;
}
bool checkAnswer(const string &nameOfTest, bool received, bool expected) {
if (received == expected) {
cout
return true;
}
cout
return false;
}