C#输入任意的一段字母字符串

C#输入任意的一段字母字符串;统计出其中大小写字母各有多少个,并输出显示第三个字母,如果其实大写直接输出,如果是小写,将其转换为大写输出

using System;

class Program {
    static void Main(string[] args) {
        Console.WriteLine("请输入任意的一段字母字符串:");
        string input = Console.ReadLine();
        int upperCount = 0, lowerCount = 0;
        char thirdChar;

        // 统计大小写字母数
        for (int i = 0; i < input.Length; i++) {
            if (Char.IsUpper(input[i])) {
                upperCount++;
            } else if (Char.IsLower(input[i])) {
                lowerCount++;
            }
        }

        // 输出大小写字母数
        Console.WriteLine("大写字母数:{0}", upperCount);
        Console.WriteLine("小写字母数:{0}", lowerCount);

        // 输出第三个字母
        if (input.Length >= 3) {
            thirdChar = input[2];
            Console.Write("第三个字母是:");
            if (Char.IsLower(thirdChar)) {
                Console.WriteLine(Char.ToUpper(thirdChar));
            } else {
                Console.WriteLine(thirdChar);
            }
        } else {
            Console.WriteLine("输入的字符串不足三个字符!");
        }
    }
}

该回答引用chatgpt:

using System;

class Program {
    static void Main() {
        Console.Write("请输入一段字母字符串:");
        string str = Console.ReadLine();

        int upperCount = 0, lowerCount = 0;
        for (int i = 0; i < str.Length; i++) {
            if (char.IsUpper(str[i])) {
                upperCount++;
            }
            else if (char.IsLower(str[i])) {
                lowerCount++;
            }
        }

        Console.WriteLine("大写字母数量:" + upperCount);
        Console.WriteLine("小写字母数量:" + lowerCount);

        if (str.Length >= 3) {
            char thirdChar = str[2];
            if (char.IsLower(thirdChar)) {
                thirdChar = char.ToUpper(thirdChar);
            }
            Console.WriteLine("第三个字母为:" + thirdChar);
        }
        else {
            Console.WriteLine("输入的字符串长度不足3个字符!");
        }
    }
}


以下内容部分参考ChatGPT模型:


首先,可以使用Console.ReadLine()方法获取用户输入的字符串。然后,使用for循环遍历字符串中的每一个字符,判断其是否为大写字母或小写字母,并分别进行计数。最后,输出大小写字母的计数结果以及第三个字母(如果其为小写字母则转换为大写字母)。

示例代码如下:

Console.Write("请输入一个字符串:");
string str = Console.ReadLine();
int upperCount = 0, lowerCount = 0;
char thirdChar = ' ';

for (int i = 0; i < str.Length; i++)
{
    if (char.IsUpper(str[i]))
    {
        upperCount++;
    }
    else if (char.IsLower(str[i]))
    {
        lowerCount++;
    }

    if (i == 2)
    {
        thirdChar = char.ToUpper(str[i]);
    }
}

Console.WriteLine("大写字母个数:{0}", upperCount);
Console.WriteLine("小写字母个数:{0}", lowerCount);
Console.WriteLine("第三个字母(大写):{0}", thirdChar);

注意,上述代码中使用了char.IsUpper()和char.IsLower()方法来判断一个字符是否为大写字母或小写字母。另外,使用char.ToUpper()方法将一个小写字母转换为大写字母。


如果我的建议对您有帮助、请点击采纳、祝您生活愉快