Free Essay

Aveff

In:

Submitted By asdfasfWE
Words 1467
Pages 6
favorite2 | 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: 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); } }java algorithm strings shareimprove this question | edited Dec 15 '13 at 10:31200_success♦
65.9k678247 | asked Dec 14 '13 at 17:21Sumeet
133115 | | | | | | If you are attempting to write as few characters as possible, you should instead go to Code Golf – Simon André Forsberg Dec 14 '13 at 17:30 | | | | related: Efficiently reverse the order of the words (not characters) in an array of characters – J.F. SebastianDec 26 '13 at 17:11 | add a comment |
6 Answers activeoldestvotes up vote3down voteaccepted | Stuffing all your code into main() is bad practice. This functionality belongs in a function of its own.When doing multiple string concatenations, you really want to use a StringBuilder. If you don't, the compiler will make one for you anyway, every time you concatenate two strings using +. It's better to do it explicitly, then, and control how many of those temporary objects are created.Your first approach works a character at a time. I suggest the following character-at-a-time approach, which allocates just one large temporary buffer. private static void reverse(char[] buf, int start, int end) { for (int i = start, j = end - 1; i < j; i++, j--) { char swap = buf[i]; buf[i] = buf[j]; buf[j] = swap; } } public static String reverseWords1(String sentence) { char[] buf = sentence.toCharArray(); // Reverse the string, character-wise reverse(buf, 0, buf.length); // Within each word, reverse the characters again int wordEnd = 0; for (int wordStart = 0; wordStart < buf.length; wordStart = wordEnd + 1) { for (wordEnd = wordStart; wordEnd < buf.length && buf[wordEnd] != ' '; wordEnd++) {} // wordStart is at the start of a word. // wordEnd is just past the end of the word. reverse(buf, wordStart, wordEnd); } return new String(buf); }I prefer your second approach, which is more succinct. Here it is, cleaned up by using a StringBuilder. public static String reverseWords2(String sentence) { StringBuilder sb = new StringBuilder(sentence.length() + 1); String[] words = sentence.split(" "); for (int i = words.length - 1; i >= 0; i--) { sb.append(words[i]).append(' '); } sb.setLength(sb.length() - 1); // Strip trailing space return sb.toString(); } shareimprove this answer | answered Dec 15 '13 at 11:48200_success♦
65.9k678247 | | | add a comment |

Did you find this question interesting? Try our newsletter
Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).
-------------------------------------------------
Top of Form Bottom of Form up vote10down vote | "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. shareimprove this answer | answered Dec 14 '13 at 18:05John Deters
67117 | | | add a comment | up vote4down vote | In your first approach you have the right idea of starting at the end of the String and working backwards. The issues you have are: * you should use StringBuilder instead of String concatenation.... * using a temp string which you add-to in reverse order, then reverse again in to the result is a slow approach .... but effective. * you should be doing it more than 1 character at a time.In your second approach, I don't like: * you have string concatenation again use a StringBuilder() * you assume all spaces are 'equal'... what about multi-space breaks "Hello There" with this code will become "There Hello"So, with a small adjustment to your regular expression (perhaps \\b ...) and conversion to StrngBuilder, I think the second option is good.The first option, if written right, will be faster than the split one (although the code is longer)....Here's an attempt at doing it by first principles: private static String reverse(String string) { if (string.isEmpty()) { return string; } int last = string.length(); StringBuilder sb = new StringBuilder(string.length()); boolean contextspace = ' ' == string.charAt(string.length() - 1); for (int i = string.length() - 1; i >= 0; i--) { if (contextspace != (string.charAt(i) == ' ')) { sb.append(string.substring(i + 1, last)); last = i + 1; contextspace = !contextspace; } } sb.append(string.substring(0, last)); return sb.toString(); } shareimprove this answer | answered Dec 14 '13 at 18:32rolfl♦
65.6k7128296 | | | add a comment |

up vote2down vote | There are many ways to do this, depending on what you're going for.If you're stuck with Java, then probably look to Apache commons: StringUtils.reverseDelimited(st, ' ');Try using Scala if you can, which is nicer in so many ways: st.split(" ").reverse.mkString(" ") shareimprove this answer | edited Dec 21 '14 at 5:50Jamal♦
24.2k780176 | answered Dec 15 '13 at 0:19Dino Fancellu
1213 | | | | | | commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/…, char) – Dino Fancellu Dec 15 '13 at 0:20 | add a comment | up vote1down vote | Here is a clean approach to reverse a string by its parts: public class Main { // ReverseParts: reverses a string by words as opposed to characters. public static String ReverseParts(String input, String splitBy, String joinBy) { StringBuilder built = new StringBuilder(); // Note: String.split uses regex. See its definition String[] list = input.split(splitBy); // Rejoin all the characters into a StringBuilder. for ( String part: list ) { built.insert(0, part); built.insert(0, joinBy); } // Remove the unnecessary 'joinBy bit'. built.delete(0, 1); // Get back the final string. return built.toString(); } public static void main(String[] args) { final String input = "Hello Johnny's World!"; System.out.println("Result: '" + ReverseParts(input, " +", " ")+"'"); // Output: // Result: 'World! Johnny's Hello' } } shareimprove this answer | edited Dec 21 '14 at 5:52Jamal♦
24.2k780176 | answered Dec 14 '13 at 18:00Scala William
23615 | | | | | | Dino's version is much better. Don't reinvent the wheel. – Scala William Dec 15 '13 at 12:15 | add a comment |

up vote-3down vote | Approach 1:JDK provides java.util.StringTokenizer to do the split or tokenizing a given text. This API helps avoid going character by character. No external jar/libraries required. Tokenizing gives you each separated word as an array element that you could print in reverse. import java.util.ArrayList; import java.util.StringTokenizer; public class reverseString { public static void main(String args[]) { StringTokenizer st = new StringTokenizer("this is a string"); ArrayList<String> arrLstStrings = new ArrayList<>(); while (st.hasMoreTokens()) { arrLstStrings.add(st.nextToken()); } for(int loop=arrLstStrings.size()-1;loop>=0;loop--) System.out.println(arrLstStrings.get(loop)); } }Approach 2: import java.util.regex.*; public class reverseString { public static void main(String args[]) { String strSource = "This is a string"; // give a max int as limit in next stmt. String[] tokens = Pattern.compile(" ").split(strSource,15) ; for (int loop=tokens.length-1;loop>=0;loop--) System.out.println(tokens[loop]); } } |

Similar Documents

Free Essay

Mr Bai

...Gate Effect on Nanowire Tunneling Field Effect Transistor (FET) Bai Ke, Department of Electrical & Computer Engineering, National University of Singapore Abstract—The simulation study on Si Nanowire Tunneling Field-Effect Transistor (TFET) has been conducted in this project. A device performance comparison was carried out between Double Gate (DG) Tunneling Field-Effect Transistor and Gate All Around Si Nanowire (GAA SiNW) Tunneling Field-Effect Transistor. The device physics and electrical characteristics of the GAA SiNW TFET are investigated for better performance of gate control and low power consumption for the future scaling applications. Due to the high electric filed generated under the gate bias GAA SiNW TFET has high Ion and steep subthreshold swing. It is shown for the first time that subthreshold swing S is proportional to the diameter of the SiNW TFET and decreasing the diameter will lead to a better Ion /Ioff ratio. Device design and physics detailing the impact of drain and source engineering was discussed for SiNW TFET for lower off-state leakage current and a higher Ion with a steeper subthreshold swing S. Lastly, we have also investigated the effect of using high-k dielectric material and shorter gate length for SiNW for the future device applications. I. INTRODUCTION gate modulation gives a better Subthreshold swing which is smaller than 60 mV/decade and Lower Ioff Leakage Current of 10-14 A/um.[1],[2] Apart from Tunneling Field-Effect Transistor (TFET), Si...

Words: 5566 - Pages: 23