黑马程序员技术交流社区
标题:
逆向输出已经输入的字符串问题
[打印本页]
作者:
硬别
时间:
2013-10-14 21:04
标题:
逆向输出已经输入的字符串问题
本帖最后由 硬别 于 2013-10-14 21:46 编辑
输入字符串例如:hello 编写一个程序输出后显示olleh,请大神们先叙述下编写这个程序的思路,如果要写下程序,麻烦请备注的详细些,先谢谢大家了。
作者:
Liu阳
时间:
2013-10-14 21:22
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 逆向输入字符串
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("输入个字符串谢谢");
string str = Console.ReadLine();
//把要反转的字符串传入反转函数
string strOut = ChangeStr(str.Trim());
Console.WriteLine(strOut);
Console.ReadKey();
}
static string ChangeStr(string str)
{
//初始化一个可变字符串用来存储反转字符串
StringBuilder strout = new StringBuilder();
//由大到小的str下标开始插入可变字符串,完成反转过程
for (int i = str.Length-1; i >= 0; i--)
{
strout.Append(str[i]);
}
return strout.ToString();//返回
}
}
}
复制代码
作者:
夢醒了⊙﹏⊙b心
时间:
2013-10-14 21:39
其实这是一个很简单的问题 呵呵 你这样考虑哈 将字符串看成以只读char数组就可以了 那么只用一个for循环倒序输出就可
Console.WriteLine("请输入您的字符串");
string strInput=Console.ReadLine();
for(int i=strInput.Length-1;i>=0;i--)
{
Console.Write(strInput[i]);
}
Console.ReadLine();
复制代码
以啦
作者:
ˋ初°见
时间:
2013-10-14 21:52
把
字符串看做成一个字符类型的数组
,用for循环遍历字符
下标
由大到小
输出就可以。例如:
string str="hello";
for(int i = str.Length-1; i >= 0; i--) //str.Length 获取字符串的长度; 减1是因为数组下标从0开始
{
Console.Write(str[i]);//输出当前遍历的字符 数组名[下标]
}
Console.WriteLine();//换行
Console.ReadKey();//暂停
复制代码
作者:
流离
时间:
2013-10-14 22:16
static void Main(string[] args)
{
string s = "hello";
for (int i = s.Length-1; i>=0; i--)
{
Console.WriteLine(s[i]);
}
Console.ReadKey();
}
for循环倒置!很多人都把他想难了
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2