JAVA 编程: 写一个方法实现将一个随机字符串的字符顺序翻转并输出到控制台,
时间: 2023-04-03 07:02:46 浏览: 81
好的,这个问题我可以回答。可以使用 StringBuilder 的 reverse() 方法来实现字符串翻转,具体代码如下:
public static void reverseString(String str) {
StringBuilder sb = new StringBuilder(str);
sb.reverse();
System.out.println(sb.toString());
}
调用这个方法,传入需要翻转的字符串即可。
相关问题
JAVA 编程: 写一个方法实现将一个随机字符串的字符顺序翻转并输出到控制台,例如将"abc"变成"cba".
可以使用以下代码实现:
```
public static void reverseString(String str) {
StringBuilder sb = new StringBuilder(str);
System.out.println(sb.reverse().toString());
}
```
调用该方法,传入随机字符串即可实现翻转并输出到控制台。例如:
```
reverseString("abc"); // 输出 "cba"
```
编程: 写一个方法实现将一个随机字符串的字符顺序翻转并输出到控制台,例如将"abc"变成"cba".
可以使用以下代码实现:
#include <stdio.h>
#include <string.h>
void reverse(char *str) {
int len = strlen(str);
for (int i = 0; i < len / 2; i++) {
char temp = str[i];
str[i] = str[len - i - 1];
str[len - i - 1] = temp;
}
}
int main() {
char str[] = "abc";
reverse(str);
printf("%s\n", str);
return 0;
}
输出结果为"cba"。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)