[倒序输出单词字符串]2
2008-03-08 17:56 Virus-BeautyCode 阅读(1204) 评论(0) 编辑 收藏 举报上回提到第二种方法,就是不使用库函数,但是思想是一样的,就是以空格来分隔,然后利用循环倒序输出,代码如下:
using System;
using System.Collections.Generic;
using System.Text;
namespace ConApp
{
class Program
{
static void Main(string[] args)
{
String temp = string.Empty;
List<string> output = new List<string>();
string input = Console.ReadLine();
input = input.Trim();
//temp = input[0].ToString();
for (int i = 0; i < input.Length; i++)
{
if (input[i] !=' ')
{
temp += input[i].ToString();
if (i == (input.Length - 1))
{
output.Add(temp);
}
}
else
{
output.Add(temp);
temp = "";
}
}
for (int i = output.Count-1 ; i >= 0; i--)
{
Console.WriteLine(output[i].ToString());
}
Console.ReadLine();
}
}
}
using System.Collections.Generic;
using System.Text;
namespace ConApp
{
class Program
{
static void Main(string[] args)
{
String temp = string.Empty;
List<string> output = new List<string>();
string input = Console.ReadLine();
input = input.Trim();
//temp = input[0].ToString();
for (int i = 0; i < input.Length; i++)
{
if (input[i] !=' ')
{
temp += input[i].ToString();
if (i == (input.Length - 1))
{
output.Add(temp);
}
}
else
{
output.Add(temp);
temp = "";
}
}
for (int i = output.Count-1 ; i >= 0; i--)
{
Console.WriteLine(output[i].ToString());
}
Console.ReadLine();
}
}
}