C# 统计给定字符串中大小写字符
检查以下条件,统计字符串中的大写字符 −
myStr[i]>='A' && myStr[i]<='Z'
检查以下条件,统计字符串中的小写字符 −
myStr[i]>='a' && myStr[i]<='z'
示例
您可以尝试运行以下代码,统计给定字符串中的大小写字符。
using System; public class Demo { public static void Main() { string myStr; int i, len, lower_case, upper_case; myStr = "Hello"; Console.Write("String: "+myStr); lower_case = 0; upper_case = 0; len = myStr.Length; for(i=0; i<len; i++) { if(myStr[i]>='a' && myStr[i]<='z') { lower_case++; } else if(myStr[i]>='A' && myStr[i]<='Z') { upper_case++; } } Console.Write("
Characters in lowecase: {0}
", lower_case); Console.Write("Characters in uppercase: {0}
", upper_case); } }
输出
String: Hello Characters in lowecase: 4 Characters in uppercase: 1
广告