黑马程序员技术交流社区
标题:
反转字符串,"hello c sharp"反转为“olleh c prahs”
[打印本页]
作者:
酱悠先生
时间:
2014-4-12 16:42
标题:
反转字符串,"hello c sharp"反转为“olleh c prahs”
本帖最后由 酱悠先生 于 2014-4-12 20:11 编辑
反转字符串,"hello c sharp"反转为“olleh c prahs”
作者:
鲤鱼
时间:
2014-4-12 17:50
本帖最后由 鲤鱼 于 2014-4-12 21:28 编辑
= =表示没审题{:3_65:} 还是坐个沙发好了
作者:
Monkey·D·Chas
时间:
2014-4-12 19:01
StringBuffer里面有直接有反转的方法
package day13;
public class ReverseString {
public static void main(String[] args) {
String s="abcde";
sop(reverse(s));
}
static String reverse (String s)
{
return reverse(s,0,s.length());
}
static String reverse(String s,int start,int end )
{
char[] ch=s.toCharArray();
for(int i=start, j=end-1;i<j;i++,j--)
swap(ch,i,j);
return new String(ch);
}
private static void swap(char[] c,int x,int y) {
char temp;
temp=c[x];
c[x]=c[y];
c[y]=temp;
}
static void sop(Object obj)
{
System.out.println(obj);
}
}
复制代码
作者:
黒■色
时间:
2014-4-12 20:18
class Program
{
static void Main(string[] args)
{
string s = null;
char[] charArray;
char temp = ' ';
string str = "hello c sharp";
string[] strArray = str.Split(' ');//把str这个字符串以空格为分隔符分开,strArray={hello,c,sharp}
for (int i = 0; i < strArray.Length; i++)
{
charArray = strArray[i].ToCharArray();//把strArray的每一个元素变为char数组,{h,e,l,l,o},{c},{s,h,a,r,p}
for (int j = 0; j < charArray.Length / 2; j++)//交换charArray[j]里的位置,交换次数为小于长度的一半
{
temp = charArray[j];
charArray[j] = charArray[charArray.Length - 1 - j];
charArray[charArray.Length - 1 - j] = temp;
}
foreach (char c in charArray)
{
s += c;
}
Console.Write(s+" ");
s = null;//将s清空,否则输出olleh ollehc ollehcprahs
}
Console.ReadKey();
}
}
复制代码
作者:
伪善者。
时间:
2014-4-12 23:48
string str = "01234 567234asdasdasl89";
char[] chr = new char[str.Length];
//定义一个字符串长度的char数组用来储存反序的各个字符的值
for (int i = str.Length - 1; i > 0; i--)
{
chr[str.Length - i - 1] = str[i];//按倒序的顺序依次给char数组赋值
}
string result = new string(chr);//用chr[] 重新生成字符串
Console.WriteLine(result);
Console.ReadKey();
复制代码
希望给你做个参考
作者:
Doublekill
时间:
2014-4-13 20:22
Console.WriteLine("请输入你要测试的字符串");
string str = Console.ReadLine();
char[] ch = new char[str.Length];
int k=0;
for (int i = 0; i < str.Length; i++)
{
ch[k] = str[i];
k++;
}
for (int j = 0; j < str.Length; j++)
{
k--;
Console.Write(ch[k]);
}
Console.ReadKey();
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2