C# 字符读取 小案例

1.检查字符串是否为有效的回文句子(考虑字母和数字,忽略标点符号、空格和大小写):

using System;
using System.Linq;

public class CharacterReaderExample
{
    public static void Main(string[] args)
    {
        string input = "A man, a plan, a canal, Panama!";
        string alphaNumericInput = new string(input.Where(char.IsLetterOrDigit).ToArray());
        string reversedInput = new string(alphaNumericInput.Reverse().ToArray());

        bool isPalindrome = string.Equals(alphaNumericInput, reversedInput, StringComparison.OrdinalIgnoreCase);
        Console.WriteLine("Is Palindrome: " + isPalindrome);
    }
}

 2.合并两个排序的字符串,保持排序顺序:

using System;

public class CharacterReaderExample
{
    public static void Main(string[] args)
    {
        string str1 = "ace";
        string str2 = "bdf";

        int i = 0, j = 0;
        string mergedString = "";

        while (i < str1.Length && j < str2.Length)
        {
            if (str1[i] < str2[j])
            {
                mergedString += str1[i];
                i++;
            }
            else
            {
                mergedString += str2[j];
                j++;
            }
        }

        while (i < str1.Length)
        {
            mergedString += str1[i];
            i++;
        }

        while (j < str2.Length)
        {
            mergedString += str2[j];
            j++;
        }

        Console.WriteLine("Merged String: " + mergedString);
    }
}

 3.将字符串中的数字字符相加(忽略非数字字符):

using System;

public class CharacterReaderExample
{
    public static void Main(string[] args)
    {
        string input = "abc123def45ghi6";

        int sum = 0;
        foreach (char c in input)
        {
            if (char.IsDigit(c))
            {
                sum += int.Parse(c.ToString());
            }
        }

        Console.WriteLine("Sum of Digits: " + sum);
    }
}

4. 检查字符串是否为回文数:

using System;

public class CharacterReaderExample
{
    public static void Main(string[] args)
    {
        string input = "madam";
        bool isPalindrome = true;

        for (int i = 0; i < input.Length / 2; i++)
        {
            if (input[i] != input[input.Length - i - 1])
            {
                isPalindrome = false;
                break;
            }
        }

        Console.WriteLine("Is Palindrome: " + isPalindrome);
    }
}

有一串字符串:12345sdfsd125@ ,用字符读取的方法把 这串字符串 里的数字提取出来,然后求和,碰到@结束程序

using System;

public class CharacterReaderExample
{
    public static void Main(string[] args)
    {
        string input = "12345sdfsd125@";
        int sum = 0;

        foreach (char c in input)
        {
            if (char.IsDigit(c))
            {
                sum += int.Parse(c.ToString());
            }
            else if (c == '@')
            {
                break;
            }
        }

        Console.WriteLine("Sum of Digits: " + sum);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值