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”


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. Below is C++ and Java implementation of the idea –


Download   Run Code


Download   Run Code


Valid sentences are –

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


1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)


Thanks for reading.

Please use our online compiler to post code in comments. To contribute, get in touch with us.
Like us? Please spread the word and help us grow. Happy coding 🙂

Leave a Reply

Notify of