Determine sentence type: Difference between revisions
Content added Content deleted
(New post.) |
|||
Line 159: | Line 159: | ||
S Serious. |
S Serious. |
||
N Neutral |
N Neutral |
||
</pre> |
|||
=={{header|C++}}== |
|||
<syntaxhighlight lang="c++"> |
|||
#include <exception> |
|||
#include <iostream> |
|||
#include <string> |
|||
#include <vector> |
|||
char sentence_type(std::string sentence) { |
|||
if ( sentence.empty() ) { |
|||
throw std::invalid_argument("Cannot classify an empty sentence"); |
|||
} |
|||
char result; |
|||
const char last_character = sentence.back(); |
|||
switch (last_character) { |
|||
case '?': result = 'Q'; break; |
|||
case '.': result = 'S'; break; |
|||
case '!': result = 'E'; break; |
|||
default: result = 'N'; break; |
|||
}; |
|||
return result; |
|||
} |
|||
int main() { |
|||
const std::vector<std::string> sentences = { "hi there, how are you today?", |
|||
"I'd like to present to you the washing machine 9001.", |
|||
"You have been nominated to win one of these!", |
|||
"Just make sure you don't break it" }; |
|||
for ( std::string sentence : sentences ) { |
|||
std::cout << sentence << " -> " << sentence_type(sentence) << std::endl; |
|||
} |
|||
} |
|||
</syntaxhighlight> |
|||
<pre> |
|||
hi there, how are you today? -> Q |
|||
I'd like to present to you the washing machine 9001. -> S |
|||
You have been nominated to win one of these! -> E |
|||
Just make sure you don't break it -> N |
|||
</pre> |
</pre> |
||