## c 连接字符_用于字符比较的C#程序

## c 连接字符

Input characters and compare them using C# program.

输入字符并使用C#程序进行比较。

Prerequisite: Methods to input a single character in C#

先决条件: 在C#中输入单个字符的方法

C#代码比较两个字符 (C# code to compare two characters)

Here, we are asking for two characters from the user – and checking whether they are the same characters or not?

在这里,我们要求用户输入两个字符-并检查它们是否相同?

// C# program for character comparison 
using System;
using System.IO;
using System.Text;

namespace IncludeHelp
{
    class Test
    {
        // Main Method 
        static void Main(string[] args)
        {
            char ch1;
            char ch2;

            //input characters
            Console.Write("Enter a character: ");
            ch1 = Console.ReadLine()[0];
            Console.Write("Enter another character: ");
            ch2 = Console.ReadLine()[0];
            
            //comparing characters
            if (ch1 == ch2)
                Console.WriteLine("Input characters are the same");
            else
                Console.WriteLine("Input characters are not the same");

            //hit ENTER to exit the program
            Console.ReadLine();
        }
    }
}

Output

输出量

First run:
Enter a character: A
Enter another character: A
Input characters are the same

Second run:
Enter a character: A
Enter another character: X
Input characters are not the same

C#代码比较字符串中的字符 (C# code to compare characters in a string)

Here, we are asking for a string from the user – and printing only vowels by comparing each character of the string with vowel characters.

在这里,我们要求用户输入字符串,并且通过将字符串的每个字符与元音字符进行比较来仅打印元音。

// C# program for character comparison 
using System;
using System.IO;
using System.Text;

namespace IncludeHelp
{
    class Test
    {
        // Main Method 
        static void Main(string[] args)
        {
            string str;
            
            //input string
            Console.Write("Enter a string: ");
            str = Console.ReadLine();

            //printing the string
            Console.WriteLine("Input string is {0}", str);

            //printing vowels 
            Console.WriteLine("Vowels are...");
            foreach (char ch in str)
            {
                if (ch == 'A' || ch == 'a' || ch == 'E' || ch == 'e' ||
                    ch == 'I' || ch == 'i' || ch == 'O' || ch == 'o' || 
                    ch == 'U' || ch == 'u')
                {
                    Console.Write(ch);
                }
            }

            //hit ENTER to exit the program
            Console.ReadLine();
        }
    }
}

Output

输出量

Enter a string: IncludeHelp.com
Input string is IncludeHelp.com
Vowels are...
Iueeo


翻译自: https://www.includehelp.com/dot-net/character-comparison-example-in-c-sharp.aspx

## c 连接字符

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值