C#程序反转字符串
我们的示例字符串是-
myStr = "Tom";
要反转字符串,首先找到字符串的长度-
//查找字符串长度 int len; len = myStr.Length - 1;
现在,使用while循环,直到长度大于0-
while (len >= 0) {
rev = rev + myStr[len];
len--;
}示例
您可以尝试运行以下代码来反转C#中的字符串。
using System;
class Demo {
static void Main() {
string myStr, rev;
myStr = "Tom";
rev ="";
Console.WriteLine("String is {0}", myStr);
//查找字符串长度
int len;
len = myStr.Length - 1;
while (len >= 0) {
rev = rev + myStr[len];
len--;
}
Console.WriteLine("Reversed String is {0}", rev);
Console.ReadLine();
}
}输出结果
String is Tom Reversed String is moT
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短