Home > Back-end >  For Java
For Java

Time:10-06

From the console receives input from the user's line of English sentences, move the front two words of a sentence to the end of a sentence and tidy sentence capitalization and punctuation, the output will be a new sentence,
Input: The dog ran through The gate and down The street.
Output: Ran through the gate and down the street, the dog?
Input: My coat was too heavy for this warm day!
Output: the Was too heavy for this warm day, my coat?

CodePudding user response:

Find the second space index, take the substring, take out the string before the index is connected to the back, finally capitalize the first char

CodePudding user response:

Interface cope {
Public String cope (String target);
}
The class newstr {
Public String newstr (String target, cope s) {
Return s.c ope (target);
}
}
The class newlast implements cope {
Private String [] STR;
Private String lastword="";
Private int last=0;
Private String oldlast="";
Private String newlast="";
Private String newstr="";
Public String cope (String target) {
String [] STR=target. The split (" \ \ s + ");
Lastword=STR [STR. Length - 1];
The last=lastword. Length () - 1;
Oldlast=lastword. Substring (last, last + 1);
Newlast=lastword. Replace (oldlast, ", ");
For (int x=2; xIf (x==2) {
String tem=STR [x]. The substring (0, 1). The toUpperCase () + STR [x] the substring (1);
Newstr +=tem + ""
} else if (x==STR. Length - 1) {
Newstr +=newlast;
} else {
Newstr +=STR [x] + "";
}
}
Newstr=newstr + STR [0]. The substring (0, 1). The toLowerCase () + STR [0]. The substring (1) + "" + STR [1] +"?" ;
Return newstr;
}
}
The class Test {
Public static void main (String [] args) {
Strings str1="The dog ran through The gate and down The street.";
String str2="My coat was too heavy for this warm day!" ;
String s1=new newstr (). Newstr (str1, new newlast ());
String s2=new newstr (). Newstr (str2, new newlast ());
System. The out. Println (s1);
System. The out. Println (s2);
}
}
  • Related