patternjavaMinor
Reverse word by word efficiency
Viewed 0 times
efficiencywordreverse
Problem
I have a method that reverse each word in a string. I want to know how I can increase it's performance and memory efficiency. Ideas I had in mind were simply using
Example:
Code:
char[] instead of strings being that strings are immutable and each time I concatenate the Strings, I am creating a new String object which is not efficient at all.Example:
hi there cow --> ih ereht wocCode:
public static String reverseWord(String str) {
int len = str.length();
String strResult = "";
String strBuffer = "";
for (int i = 0; i < len; i++) {
if (str.charAt(i) != ' ') {
strBuffer = str.charAt(i) + strBuffer;
}
else if (str.charAt(i) == ' ') {
strResult += strBuffer + " ";
strBuffer = "";
}
}
strResult += strBuffer;
return strResult;
}Solution
-
Ideas I had in mind were simply using char[] instead of strings being that strings are immutable and each time I concatenate the Strings, I am creating a new String object which is not efficient at all.
Yes, that will be faster. Go ahead and write it :-)
-
Instead of
-
I'd rename
-
The following is the same, you don't need the second, inverted condition:
-
You might want to use a more another check for spaces. Currently it handles tabs (
will be
Here is a slightly improved version with
Ideas I had in mind were simply using char[] instead of strings being that strings are immutable and each time I concatenate the Strings, I am creating a new String object which is not efficient at all.
Yes, that will be faster. Go ahead and write it :-)
-
Instead of
Strings like strResult and strBuffer you could use StringBuilders (although using char is probably faster). StringBuilder is mutable and would be faster than using Strings. StringBuilder also supports insert(offset, char) but also has a reverse method.-
I'd rename
strResult to result (to avoid Hungarian notation) and strBuffer to the more descriptive currentWord.-
if (str.charAt(i) != ' ') {
...
}
else if (str.charAt(i) == ' ') {
...
}The following is the same, you don't need the second, inverted condition:
if (str.charAt(i) != ' ') {
...
} else {
...
}-
You might want to use a more another check for spaces. Currently it handles tabs (
\t) as normal characters and"hi\tthere\tcow"will be
"woc\tereht\tih"Here is a slightly improved version with
StringBuilders:public static String reverseWord(String str) {
int len = str.length();
StringBuilder result = new StringBuilder();
StringBuilder currentWord = new StringBuilder();
for (int i = 0; i < len; i++) {
if (str.charAt(i) != ' ') {
currentWord.insert(0, str.charAt(i));
} else {
result.append(currentWord).append(" ");
currentWord.setLength(0);
}
}
result.append(currentWord);
return result.toString();
}Code Snippets
if (str.charAt(i) != ' ') {
...
}
else if (str.charAt(i) == ' ') {
...
}if (str.charAt(i) != ' ') {
...
} else {
...
}"hi\tthere\tcow""woc\tereht\tih"public static String reverseWord(String str) {
int len = str.length();
StringBuilder result = new StringBuilder();
StringBuilder currentWord = new StringBuilder();
for (int i = 0; i < len; i++) {
if (str.charAt(i) != ' ') {
currentWord.insert(0, str.charAt(i));
} else {
result.append(currentWord).append(" ");
currentWord.setLength(0);
}
}
result.append(currentWord);
return result.toString();
}Context
StackExchange Code Review Q#44320, answer score: 6
Revisions (0)
No revisions yet.