实现字符串的逆置。比如输入字符串“helloworld"存储到s中,经过逆置变换,s变为"dlrowolleh"并输出。

来源:百度知道 编辑:UC知道 时间:2024/06/04 07:53:37

dev c++下运行成功.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main()
{
char s[1024],*t;
printf("请输入字符串:");
gets(s);
t=strrev(s);
printf("\n倒置后的字符串为:%s\n\n",t);
system("pause");
}

public class Reverse {
/**
* 逆序输出字符串
* @param s
* @return
*/
public static String StringReverse(String s){

//根据字符串s的长度定义char数组的长度
char[] chs = new char[s.length()];
//循环截取字符,逆序赋值给字符数组chs
for(int i=0;i<s.length();i++){
int j = s.length()-1-i;
chs[j] = s.charAt(i);
}
//根据chs数组构造新字符串s1
String s1 = new String(chs);
System.out.println(s1);
return s1;

}
/**
* 测试方法
* @param args
*/
public static void main(String[] args) {
String s = "1234567";
StringR