In this article, we will arrange the words in the array in reverse order, such as how old are you-> your are old
Sample Program Output results:
The A:
How old are for you!? I Don ' t understand
The second:
Understand don ' t I?! are old
Sample Code
public static void Main (string[] args) {char[] chars= the new String ("How old are you!?"
I Don ' t understand "). ToCharArray ();
System.out.println ("The" the "the");
System.out.println (chars); Reversewords (chars);
Main method System.out.println ("The Second:");
System.out.println (chars); The/** * will arrange the words in the array in reverse order such as how old are you-> your are old how * @param chars/public static void re
Versewords (char[] chars) {reversechars (chars,0,chars.length-1);
int begin =-1;
int end = 0;
for (int i=0;i<chars.length;i++) {char c = chars[i]; if ((c>= ' a ' &&c<= ' z ') | | (c>= ' A ' &&c<= ' Z ') | |
c== ' \ ') {//Simple judgment of whether the word is contiguous if (begin==-1) {begin = i;
End=i;
}else{end=i;
if (i==chars.length-1) {reversechars (chars,begin,end);
}}else{if (begin!=-1) {reversechars (chars,begin,end);
Begin=-1; End=0;
/** * will arrange char in a range of characters in reverse order such as Hello-> Olleh * @param chars array * @param begin position
* @param End Position */public static void Reversechars (char[] chars, int begin, int ended) {while (End>begin) {
char C = chars[begin];
Chars[begin] = Chars[end];
Chars[end] = c;
begin++;
end--; }
}
The above is the use of Java to achieve the reverse order of words, I hope to be able to understand that everyone has helped