Check if given sentence is syntactically correct or not


Given a simple sentence and a set of syntax rules, validate if it is syntactically correct or not.

 
A simple sentence if syntactically correct if it fulfills given rules. The following are given rules.

  • Sentence must start with a Uppercase character (e.g. Noun/ I/ We/ He etc.)
  • There must be spaces between words.
  • Then the sentence must end with a full stop(.).
  • Two continuous spaces are not allowed.
  • Two continuous uppercase characters are not allowed.
  • However the sentence can end after an uppercase character.
For example,


“This sentence is syntactically correct.”

“This sentence is syntactically incorrect as two continuous spaces are not allowed.”

“This sentence is syntactically correct Y.”

“This sentence is syntactically incorRect as uppercase character is not allowed midway of the string.”

“THis sentence is syntactically incorrect as lowercase character don’t follow the first uppercase character.”

“This sentence is syntactically incorrect as it doesn’t end with a full stop”


This question is taken from here.
 


 
The idea is to scan the given string and check for the above rules by comparing adjacent characters. If any of the given rules is violated, then return false.

 
C++ implementation –
 

Download   Run Code

Output:

Valid sentences are –

This sentence is syntactically correct.
This sentence is syntactically correct Y.

 

Thanks for reading.




Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂