分别使用java和python实现字符串反转

1.java

public class StringReverse {
    public static void main(String[] args) {
        String str = "HelloWorld!";
        String reversedStr = reverseString(str);
        System.out.println(reversedStr);
    }

    public static String reverseString(String str) {
        StringBuilder sb = new StringBuilder();
        for (int i = str.length() - 1; i >= 0; i--) {
            sb.append(str.charAt(i));
        }
        return sb.toString();
    }
}

2. python

def reverse_string(str):
    return str[::-1]

str = "HelloWorld!"

reversed_str = reverse_string(str)
print(reversed_str)

你可能感兴趣的:(python,java)