Created
October 28, 2015 13:28
-
-
Save OlgaKulikova/e6cb31f5e23665487ab4 to your computer and use it in GitHub Desktop.
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
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