bob exercise, all tests are passed
This commit is contained in:
parent
f93f099cc6
commit
c252a1a93d
5 changed files with 273 additions and 0 deletions
57
cpp/bob/bob.cpp
Normal file
57
cpp/bob/bob.cpp
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
#include "bob.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <locale>
|
||||
#include <string>
|
||||
|
||||
namespace bob {
|
||||
|
||||
|
||||
using namespace std;
|
||||
|
||||
namespace {
|
||||
|
||||
void trim_spaces(string &str)
|
||||
{
|
||||
str.erase(remove_if(str.begin(), str.end(),
|
||||
[](char c){return isspace(static_cast<unsigned char>(c));}),
|
||||
str.end());
|
||||
}
|
||||
|
||||
string to_uppercase(const string &str)
|
||||
{
|
||||
string result = str;
|
||||
for (auto & c: result)
|
||||
c = toupper(c);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool has_alpha(const string &str)
|
||||
{
|
||||
return find_if(str.begin(), str.end(),
|
||||
[](char c){return isalpha(static_cast<unsigned char>(c));}) != str.end();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
const char * hey(const char input[])
|
||||
{
|
||||
|
||||
string str(input);
|
||||
trim_spaces(str);
|
||||
|
||||
if (str.empty())
|
||||
return "Fine. Be that way!";
|
||||
|
||||
if (has_alpha(str) && str == to_uppercase(str))
|
||||
return "Whoa, chill out!";
|
||||
|
||||
if ('?' == str.back())
|
||||
return "Sure.";
|
||||
|
||||
return "Whatever.";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue