class
GFG
{
static
boolean
checkSentence(
char
[] str)
{
int
len = str.length;
if
(str[
0
] <
'A'
|| str[
0
] >
'Z'
)
return
false
;
if
(str[len -
1
] !=
'.'
)
return
false
;
int
prev_state =
0
, curr_state =
0
;
int
index =
1
;
while
(index <= str.length)
{
if
(str[index] >=
'A'
&& str[index] <=
'Z'
)
curr_state =
0
;
else
if
(str[index] ==
' '
)
curr_state =
1
;
else
if
(str[index] >=
'a'
&& str[index] <=
'z'
)
curr_state =
2
;
else
if
(str[index] ==
'.'
)
curr_state =
3
;
if
(prev_state == curr_state && curr_state !=
2
)
return
false
;
if
(prev_state ==
2
&& curr_state ==
0
)
return
false
;
if
(curr_state ==
3
&& prev_state !=
1
)
return
(index +
1
== str.length);
index++;
prev_state = curr_state;
}
return
false
;
}
public
static
void
main(String[] args)
{
String[] str = {
"I love cinema."
,
"The vertex is S."
,
"I am single."
,
"My name is KG."
,
"I lovE cinema."
,
"GeeksQuiz. is a quiz site."
,
"I love Geeksquiz and Geeksforgeeks."
,
" You are my friend."
,
"I love cinema"
};
int
str_size = str.length;
int
i =
0
;
for
(i =
0
; i < str_size; i++)
{
if
(checkSentence(str[i].toCharArray()))
System.out.println(
"\""
+ str[i] +
"\""
+
" is correct"
);
else
System.out.println(
"\""
+ str[i] +
"\""
+
" is incorrect"
);
}
}
}