using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CompareString
{
class Program
{
static void Main(string[] args)
{
string strX, strY; //定义变量用来记录输入的字符串
int x;
//输入字符串的值
Console.Write("请输入字符串1的值:");
strX = Console.ReadLine();
Console.Write("请输入字符串2的值:");
strY = Console.ReadLine();
//循环比较两个字符串相应位置上的字符
x = bz(strX,strY);
if (x == 2)
{
Console.WriteLine("字符串{0} 小于 字符串{1}", strX, strY);
}
//字符串1与字符串2前面的字符全相同,且字符串1的长度大于字符串2的长度
if (x == 1)
{
Console.WriteLine("字符串{0} 大于 字符串{1}", strX, strY);
}
//字符串1与字符串2前面的字符全相同,且字符串1的长度等于字符串2的长度
if (x == 0)
{
Console.WriteLine("字符串{0} 等于 字符串{1}", strX, strY);
}
Console.ReadKey(true);
}
public static int bz(string strInput1, string strInput2)
{
int i;
for (i = 0; i < strInput1.Length && i < strInput2.Length; i++)
{
if (strInput1[i] > strInput2[i])
{
return 1;
}
if (strInput1[i] < strInput2[i])
{
return 2;
}
}
//字符串1与字符串2前面的字符全相同,且字符串1的长度小于字符串2的长度
if (i == strInput1.Length && i < strInput2.Length)
{
return 2;
}
//字符串1与字符串2前面的字符全相同,且字符串1的长度大于字符串2的长度
if (i < strInput1.Length && i == strInput2.Length)
{
return 1;
}
//字符串1与字符串2前面的字符全相同,且字符串1的长度等于字符串2的长度
if (i == strInput1.Length && i == strInput2.Length)
{
return 0;
}
return 0;
}
}
}