4

样本输入:

abc def ghi

样本输出:

Cba Fed Ihg

这是我的代码:

import java.util.Stack;

public class StringRev {
    static String output1 = new String();
    static Stack<Character> stack = new Stack<Character>();
    public static void ReverseString(String input) {
        input = input + " ";
        for (int i = 0; i < input.length(); i++) {
            boolean cap = true;
            if (input.charAt(i) == ' ') {
                while (!stack.isEmpty()) {
                    if (cap) {
                        char c = stack.pop().charValue();
                        c = Character.toUpperCase(c);
                        output1 = output1 + c;
                        cap = false;
                    } else
                        output1 = output1 + stack.pop().charValue();
                }
                output1 += " ";
            } else {
                stack.push(input.charAt(i));
            }
        }
        System.out.print(output1);
    }
}

有更好的解决方案吗?

4

6 回答 6

5

利用

StringBuilder#reverse()

然后不添加任何第三方库,

    String originalString = "abc def ghi";
    StringBuilder resultBuilder = new StringBuilder();
    for (String string : originalString.split(" ")) {
        String revStr = new StringBuilder(string).reverse().toString();
        revStr = Character.toUpperCase(revStr.charAt(0))
                + revStr.substring(1);
        resultBuilder.append(revStr).append(" ");
    }
    System.out.println(resultBuilder.toString());  //Cba Fed Ihg 

有一个演示

于 2013-10-18T10:36:37.603 回答
2

您可以使用StringBuffertoreverse()字符串。

然后使用该WordUtils#capitalize(String)方法将字符串的第一个字母大写。

String str = "abc def ghi";
StringBuilder sb = new StringBuilder();
for (String s : str.split(" ")) {
    String revStr = new StringBuffer(s).reverse().toString();
    sb.append(WordUtils.capitalize(revStr)).append(" ");
}
String strReversed = sb.toString();
于 2013-10-18T10:34:15.210 回答
1

已编辑

首先反转字符串并将第一个字符变为大写

String string="hello jump";
StringTokenizer str = new StringTokenizer(string," ") ;
String finalString ;

while (str.hasMoreTokens()) {
   String input = str.nextToken() ;
   String reverse = new StringBuffer(input).reverse().toString();
   System.out.println(reverse);

   String output = reverse .substring(0, 1).toUpperCase() + reverse .substring(1);
   finalString=finalString+" "+output ;
}

System.out.println(finalString);
于 2013-10-18T10:32:38.710 回答
1
public static String reverseString(final String input){
    if(null == input || isEmpty(input))
        return "";

    String result = "";
    String[] items = input.split(" ");

    for(int i = 0; i < items.length; i++){
        result += new StringBuffer(items[i]).reverse().toString();
    }

    return result.substring(0,1).toupperCase() + result.substring(1);
}
于 2013-10-18T10:34:23.400 回答
1

1)用这个反转字符串

StringBuffer a = new StringBuffer("Java");
a.reverse();

2) 使首字母大写使用 apache commons lang 包 org.apache.commons.lang.StringUtils 中的 StringUtils 类

它使首字母大写

capitalise(String);

希望能帮助到你。

于 2013-10-18T10:40:09.117 回答
0
import java.util.*;
public class CapatiliseAndReverse {
    public static void reverseCharacter(String input) {
        String result = "";
        StringBuilder revString = null;

        String split[] = input.split(" ");

        for (int i = 0; i < split.length; i++) {
            revString = new StringBuilder(split[i]);
            revString = revString.reverse();

            for (int index = 0; index < revString.length(); index++) {

                char c = revString.charAt(index);
                if (Character.isLowerCase(revString.charAt(0))) {
                    revString.setCharAt(0, Character.toUpperCase(c));
                }

                if (Character.isUpperCase(c)) {
                    revString.setCharAt(index, Character.toLowerCase(c));
                }
            }

            result = result + " " + revString;
        }

        System.out.println(result.trim());
    }

    public static void main(String[] args) {
        //System.out.println(reverseCharacter("old is GlOd"));
        Scanner sc = new Scanner(System.in);
        reverseCharacter(sc.nextLine());
    }
}
于 2017-03-16T09:29:48.300 回答