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);
}
}