黑马程序员技术交流社区
标题:
i love you”--> “i evol uoy 反序输出每个单词(有木有朋友抽.....
[打印本页]
作者:
胡元江
时间:
2013-2-6 14:17
标题:
i love you”--> “i evol uoy 反序输出每个单词(有木有朋友抽.....
本帖最后由 胡元江 于 2013-2-13 20:04 编辑
3.
“i love you”--> “i evol uoy”
Console.WriteLine("您好。请输入一个字符串");
string str1 = Console.ReadLine();
string str3 = "";
string[] strs = str1.Split(new char[] { ' ' },
StringSplitOptions.RemoveEmptyEntries); //分隔取出每一个单词 成为一个字符串数组
for (int i = 0; i < strs.Length; i++)
{
string str2 = strs
; //将字符串数组中的每一个元素分别赋给str2
for (int j = str2.Length - 1; j >= 0; j--) //倒序输入每个单词
{
str3 += str2[j];
}
str3 += " ";
}
Console.WriteLine(str3);
Console.ReadKey();
作者:
万建华
时间:
2013-2-6 15:07
没有必要那么麻烦把 那那个分成3个字符串处理 不更简单一点 ?
作者:
王晨
时间:
2013-2-6 21:52
本帖最后由 王晨 于 2013-2-6 21:54 编辑
我觉得这样写更加简便一些,Lz可以看一下
static void Main(string[] args)
{
string arr = "I love you",result,new_result = "";//new_result存放最终结果
arr = arr.ToLower();//通过tolower函数将其转成小写
string[] arrtemp = arr.Split();//使用split函数进行分割
for (int i = 0; i < arrtemp.Length; i++)
{
char[] new_arrtemp = arrtemp[i].ToCharArray();
Array.Reverse(new_arrtemp);//使用array.reverse将其倒序输出
result = new string(new_arrtemp);
new_result += result + " ";
}
Console.WriteLine(new_result);
Console.ReadKey();
}
复制代码
作者:
﹏_、
时间:
2013-2-9 21:30
Console.WriteLine("您好。请输入一个字符串");
string str1 = Console.ReadLine();
string str3 = "";
string[] strs = str1.Split(new char[] { ' ' },
StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i <= strs.Length/2 ; i++)
{
string stn = strs[strs.Length - i-1];
strs[strs.Length-i-1] = strs
;
strs
= stn;
}
for (int i = 0; i < strs.Length; i++)
{
string str2 = strs
;
for (int j = str2.Length - 1; j >= 0; j--)
{
str3 += str2[j];
}
str3 += " ";
}
Console.WriteLine(str3);
Console.ReadKey();
i
你加上那段红的应该就没问题了~~·你只是把这个大字符串中的小字符串中的字符倒过来了但是这个大字符传中的小字符串没倒过来
作者:
胡元江
时间:
2013-2-13 09:54
额。我的代码只是将一句英文中的所有单词倒序,因为题目要求是这样。不需要将整个句子反序输出
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2