patternjavaModerate
Reversing words in a string
Viewed 0 times
wordsstringreversing
Problem
I have to reverse the string "He is the one" to "one the is He". I have written some programs in Java but am looking for other best solutions. Suggest any possible ways to minimize the current program.
First approach:
Second approach:
First approach:
class StringRev{
public static void main(String args[]){
String str = "He is the one";
String temp = "";
String finalString = "";
for(int i =str.length()-1;i>=0;i--){
temp +=i!=0?str.charAt(i):str.charAt(i)+" ";
if(str.charAt(i) == ' '||i==0){
for(int j=temp.length()-1;j>=0;j--){
finalString += temp.charAt(j);
}
temp = "";
}
}
System.out.println(finalString);
}
}Second approach:
class StringRev2{
public static void main(String args[]){
String str[] = "He is the one".split(" ");
String finalStr="";
for(int i = str.length-1; i>= 0 ;i--){
finalStr += str[i]+" ";
}
System.out.println(finalStr);
}
}Solution
"Minimize" is a really bad word to use when trying to consider the quality of your code. It's vague, and it can lead to undesirable outcomes, such as broken or unusable code.
For example, is your goal to minimize the number of bytes used storing the source code on a hard drive? That might have been important when paper tape was a popular storage medium, but it's irrelevant today. It's so irrelevant that the difference between these two files is less than the size of a sector, so both actually occupy the same amount of storage space.
Is your goal to reduce the number of CPU cycles spent? Given that cell phones have 100MHz processors these days, you need to do more work to figure out if the effort you expend thinking about the problem will ever be made up for in efficiency experienced by your users. If this is run once or twice, efficiency simply is not important. On the other hand, if it could ever become part of a networking protocol, efficiency is extremely important. Quite honestly, English word reversal seems to be too specialized to fit the test of practicality for efficiency.
In general, what most people should be striving for in their code is "correctness" and "clarity". You want to know that it works correctly in all situations. The answer to that is to write unit tests. For clarity, you want the code to be readable, understandable, and usable. Make sure you have chosen good names. Modularize the functions. For example, you should consider extracting the dependency on System.out.println, as outputting the string has nothing to do with reversing the string.
For example, is your goal to minimize the number of bytes used storing the source code on a hard drive? That might have been important when paper tape was a popular storage medium, but it's irrelevant today. It's so irrelevant that the difference between these two files is less than the size of a sector, so both actually occupy the same amount of storage space.
Is your goal to reduce the number of CPU cycles spent? Given that cell phones have 100MHz processors these days, you need to do more work to figure out if the effort you expend thinking about the problem will ever be made up for in efficiency experienced by your users. If this is run once or twice, efficiency simply is not important. On the other hand, if it could ever become part of a networking protocol, efficiency is extremely important. Quite honestly, English word reversal seems to be too specialized to fit the test of practicality for efficiency.
In general, what most people should be striving for in their code is "correctness" and "clarity". You want to know that it works correctly in all situations. The answer to that is to write unit tests. For clarity, you want the code to be readable, understandable, and usable. Make sure you have chosen good names. Modularize the functions. For example, you should consider extracting the dependency on System.out.println, as outputting the string has nothing to do with reversing the string.
Context
StackExchange Code Review Q#37364, answer score: 11
Revisions (0)
No revisions yet.