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
广告