62,628
社区成员
发帖
与我相关
我的任务
分享
public class ReverseOutput {
public static void main(String[] args) {
String s = "hello world.how are you?i fine.and you?nice to meet you!me to!";
new Reverse().doRoput(s);
}
}
class Reverse {
Reverse() {}
public void doRoput(String str) {
//在这里写实现代码
}
}
import java.util.*;
import java.util.regex.*;
//....
public void doRoput(String str) {
Stack<String> reverseString=new Stack<String>();
String regex="[^\\w]";
Matcher m=Pattern.compile(regex).matcher(str);
int start=0,end=0;
while(m.find()){
if(start==m.start()){
reverseString.push(m.group());
}else{
reverseString.push(str.substring(start,m.start()));
reverseString.push(m.group());
}
start=m.end();
}
if(start!=str.length()-1){
reverseString.push(str.substring(start));
}
while(!reverseString.empty()){
System.out.print(reverseString.pop());
}
System.out.println();
}
import java.util.*;
public class Palindrome1
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string: ");
String word = sc.next();
int i = word.length();
int j = 0;
while (j <= (i / 2) -1 && word.charAt(j) == word.charAt(i - j - 1))
j++;
if (j == i / 2)
System.out.println("The string is palindrome.");
else
System.out.println("The string is not palindrome.");
}
}
import java.util.*;
public class Palindrome2
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string: ");
String word = sc.next();
if (word.equals(new StringBuffer(word).reverse().toString()))
System.out.println("The string is palindrome.");
else
System.out.println("The string is not palindrome.");
}
}
public class ReverseOutput {
public static void main(String[] args) {
String s = "hello world.how are you?i fine.and you?nice to meet you!me to!";
new Reverse().doRoput(s);
}
}
class Reverse {
Reverse() {}
public void doRoput(String str) {
//在这里写实现代码
int i = str.length();
while(i > 0){
System.out.print(str.substring(i-1,i));
i--;
}
}
}
public class MyString {
public static void main(String[] args) {
String string="Hello Word da jia hao !";
String[] strs=string.split(" ");
StringBuilder sb=new StringBuilder();
for(int i=strs.length-1;i>0;i--){
sb.append(strs[i]+" ");
}
sb.append(strs[0]);
String newStr=sb.toString();
System.out.println(newStr);
}
/**
* 如果把
* for(int i=strs.length-1;i>0;i--){
* sb.append(strs[i]+" ");
* }
* sb.append(strs[0]);
* 换成
* for(int i=strs.length-1;i>=0;i--){
* sb.append(strs[i]+" ");
* }
* sb就会在字符串结尾多出一个空格,要去掉的
*
*/
}