Skip to content

Instantly share code, notes, and snippets.

@OlgaKulikova
Created October 28, 2015 13:28
Show Gist options
  • Save OlgaKulikova/e6cb31f5e23665487ab4 to your computer and use it in GitHub Desktop.
Save OlgaKulikova/e6cb31f5e23665487ab4 to your computer and use it in GitHub Desktop.
public class ReverseString {
public static void main(String[] args) {
String oldString = "abc1d2e fgh3i";
String[] words = oldString.split(" ");
String alphabet = "abcdefghijklmnopqrstuvwxyz";
StringBuilder sentenceBuilder = new StringBuilder();
for (String word : words) {
StringBuilder wordBuilder = new StringBuilder();
char[] unletterSymbols = new char[word.length()];
for (int i = 0; i < word.length(); i++) {
for (int j = 0; j < alphabet.length(); j++) {
if (word.charAt(i) == alphabet.charAt(j)) {
wordBuilder.append(word.charAt(i));
break;
}
if (j == alphabet.length() - 1) {
unletterSymbols[i] = word.charAt(i);
}
}
}
wordBuilder.reverse();
for (int i = 0; i < unletterSymbols.length; i++) {
if (unletterSymbols[i] != 0) {
wordBuilder.insert(i, unletterSymbols[i]);
}
}
sentenceBuilder.append(wordBuilder.toString())
.append(" ");
}
System.out.println(sentenceBuilder.toString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment