要计算字符串中的大写字符,请检查以下条件-
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("\nCharacters in lowecase: {0}\n", lower_case); Console.Write("Characters in uppercase: {0}\n\n", upper_case); } }
输出结果
String: Hello Characters in lowecase: 4 Characters in uppercase: 1