• 编程实践63


    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;
            }
        }
    }
  • 相关阅读:
    面试题 01.04. 回文排列
    面试题 01.03. URL化
    面试题 01.02. 判定是否互为字符重排
    面试题 01.01. 判定字符是否唯一
    剑指 Offer 68
    剑指 Offer 68
    Wpf杀死所有线程、Wpf关闭程序杀死所有线程
    wpf的webbrowser与javascript交互
    WPF将HHMMSS转换为时间格式字符串
    IDEA建立Spring MVC Hello World 详细入门教程
  • 原文地址:https://www.cnblogs.com/Wzqa/p/2812372.html
Copyright © 2020-2023  润新知