Created
December 16, 2021 18:42
-
-
Save aadipoddar/7d1ff6893dc1c2ba85ac73dc0c13d36b to your computer and use it in GitHub Desktop.
School Program Palindrome Sentence Convert
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Write a program to accept a sentence which may be terminated by either '.', '?' or '!' | |
only. The words are to be entered by s asingle blank space and re in UPPER CASE. | |
Perform the following tasks: | |
(a) Check for validity for the entered senctence. | |
(b) Convert the non-palidrome words of the sentence into palidrome words by | |
concatenating the word by its revers (excluding the last character). | |
Example: The reverse of the word HELP would be LEH (ommiting the | |
last alphabet) and by concatenating both, the new palidrome word is HELPLEH. | |
Thus the word HELP become HELPLEH | |
Note: The words which end with repeated alphabets, for exaple ABB would | |
becoem ABBA and not ABBBA and XAZZZ becomes XAZZZAX. | |
(c) Display the original sentence along with converted sentence. | |
*/ | |
import java.util.Scanner; | |
class Sent_Modi | |
{ | |
public static void main() | |
{ | |
Scanner sc = new Scanner(System.in); | |
System.out.println("Enter the sentence"); | |
String sentence = sc.nextLine(); | |
char character = ' '; | |
String word = ""; | |
String finalSentence = ""; | |
String repeatedWord = ""; | |
for(int i = 0; i < sentence.length(); i++) | |
{ | |
character = sentence.charAt(i); | |
if(character == '.' || character == '?' || character == '!') | |
{ | |
break; | |
} | |
if(character != ' ') | |
{ | |
word = word + character; | |
} | |
if(character == ' ' || i == (sentence.length() - 1)) | |
{ | |
if(word.length() == 1) | |
{ | |
finalSentence = finalSentence + word + " "; | |
word = ""; | |
repeatedWord = ""; | |
continue; | |
} | |
int count = 0; | |
for(int j = 1; j < word.length(); j++) | |
{ | |
if(word.charAt(j) == word.charAt(j-1)) | |
{ | |
count++; | |
} | |
} | |
if(word.charAt(word.length() - 1) == word.charAt(word.length() - 2)) | |
{ | |
repeatedWord = word.substring(word.length() - count - 1, word.length()); | |
word = word.substring(0, word.length() - count - 1); | |
} | |
String reverse = ""; | |
for(int j = word.length() - 1; j >= 0; j--) | |
{ | |
reverse = reverse + word.charAt(j); | |
} | |
if(word.equals(reverse)) | |
{ | |
if(repeatedWord != "") | |
{ | |
finalSentence = finalSentence + word + repeatedWord + word + " "; | |
word = ""; | |
repeatedWord = ""; | |
continue; | |
} | |
else | |
{ | |
finalSentence = finalSentence + word + " "; | |
word = ""; | |
repeatedWord = ""; | |
continue; | |
} | |
} | |
if(!word.equals(reverse)) | |
{ | |
if(repeatedWord != "") | |
{ | |
finalSentence = finalSentence + word + repeatedWord + reverse + " "; | |
} | |
else | |
{ | |
word = word.substring(0, word.length() - 1); | |
finalSentence = finalSentence + word + reverse + " "; | |
} | |
} | |
word = ""; | |
repeatedWord = ""; | |
} | |
} | |
System.out.println(finalSentence); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment