Skip to content

Instantly share code, notes, and snippets.

@imprakharshukla
Created February 6, 2020 16:32
Show Gist options
  • Save imprakharshukla/7df7c7127087f42ab09b1cc83bdcd7f4 to your computer and use it in GitHub Desktop.
Save imprakharshukla/7df7c7127087f42ab09b1cc83bdcd7f4 to your computer and use it in GitHub Desktop.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.SQLOutput;
public class Q3 {
static String s;
static String s2 = "";
public static void main(String[] args) throws IOException {
InputStreamReader ir = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(ir);
System.out.println("Enter the sentence please.");
s = br.readLine();
String word = "";
for (int i = 0; i < s.length(); ++i) {
char ch = s.charAt(i);
if (ch != ' ' && ch != '!' && ch != '?' && ch != '.') {
word = word + ch;
} else {
//We have the word now to process
String temp = palendrome(word) + " ";
s2 = s2 + temp;
word = "";
}
}
System.out.println(s2);
}
static String palendrome(String g) {
if (!isEndRepeat(g)) {
return g + palendromeReturn(g);
} else {
return repeatRemover(g) + palendromeReturn(g);
}
}
static boolean isEndRepeat(String l) {
return l.charAt(l.length() - 2) == l.charAt(l.length() - 1);
}
static String palendromeReturn(String m) {
String w = "";
for (int i = m.length()-2; i>=0 ; --i) {
char chh = m.charAt(i);
w = w + chh;
}
return w;
}
static String repeatRemover(String j) {
int p = 0;
for (int i = j.length() - 1; i >= 0; --i) {
char n = j.charAt(i);
if (n != j.charAt(j.length() - 1)) {
p = i+2;
break;
}
}
return j.substring(0, p);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment