【C#】字符串处理器

实现:

  1. 统计字符串中单词的数量。
  2. 查找字符串中最长的单词,并显示其长度。
  3. 将字符串中的所有单词首字母大写。
  4. 将字符串中的所有单词反转。

要求:

  • 使用面向对象的方式实现,包括至少一个类(例如 StringProcessor)。
  • 不使用现成的字符串处理函数(例如 Split、Reverse 等),自行实现相应的功能。
  • 提供命令行界面,不需要图形用户界面。

代码实现:

1.定义StringProcessor类

public class StringProcessor
{
    private string _input;

    public StringProcessor(string input)
    {
        _input = input;
    }
}

2.统计字符串中单词的数量。

// 统计字符串中单词的数量
public int CountWords()
{
    int count = 0;
    bool inWord = false;

    foreach (char c in _input)
    {
        if (char.IsWhiteSpace(c))
        {
            inWord = false;
        }
        else
        {
            if (!inWord)
            {
                count++;
                inWord = true;
            }
        }
    }

    return count;
}

3.查找字符串中最长的单词,并显示其长度。

// 查找字符串中最长的单词,并显示其长度
public (string word, int length) FindLongestWord()
{
    int maxLength = 0;
    string longestWord = "";
    string currentWord = "";

    foreach (char c in _input)
    {
        if (char.IsWhiteSpace(c))
        {
            if (currentWord.Length > maxLength)
            {
                maxLength = currentWord.Length;
                longestWord = currentWord;
            }
            currentWord = "";
        }
        else
        {
            currentWord += c;
        }
    }

    if (currentWord.Length > maxLength)
    {
        maxLength = currentWord.Length;
        longestWord = currentWord;
    }

    return (longestWord, maxLength);
}

4.将字符串中的所有单词首字母大写。

// 将字符串中的所有单词首字母大写
public string CapitalizeWords()
{
    char[] result = new char[_input.Length];
    bool newWord = true;

    for (int i = 0; i < _input.Length; i++)
    {
        if (char.IsWhiteSpace(_input[i]))
        {
            result[i] = _input[i];
            newWord = true;
        }
        else
        {
            if (newWord && char.IsLetter(_input[i]))
            {
                result[i] = char.ToUpper(_input[i]);
                newWord = false;
            }
            else
            {
                result[i] = _input[i];
            }
        }
    }

    return new string(result);
}

5. 将字符串中的所有单词反转。

// 将字符串中的所有单词反转
public string ReverseWords()
{
    char[] result = new char[_input.Length];
    int start = 0, end = 0;

    while (start < _input.Length)
    {
        while (end < _input.Length && !char.IsWhiteSpace(_input[end]))
        {
            end++;
        }

        int wordEnd = end - 1;
        for (int i = start; i < end; i++)
        {
            result[i] = _input[wordEnd--];
        }

        while (end < _input.Length && char.IsWhiteSpace(_input[end]))
        {
            result[end] = _input[end];
            end++;
        }

        start = end;
    }

    return new string(result);
}

6.main函数

 static void Main(string[] args)
 {
     Console.WriteLine("请输入一个字符串:");
     string input = Console.ReadLine();

     StringProcessor processor = new StringProcessor(input);

     Console.WriteLine("单词数量:" + processor.CountWords());

     var (word, length) = processor.FindLongestWord();
     Console.WriteLine($"最长的单词:{word}, 长度:{length}");

     Console.WriteLine("首字母大写:" + processor.CapitalizeWords());

     Console.WriteLine("单词反转:" + processor.ReverseWords());
 }

 7.完整代码

using System;

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("请输入一个字符串:");
        string input = Console.ReadLine();

        StringProcessor processor = new StringProcessor(input);

        Console.WriteLine("单词数量:" + processor.CountWords());

        var (word, length) = processor.FindLongestWord();
        Console.WriteLine($"最长的单词:{word}, 长度:{length}");

        Console.WriteLine("首字母大写:" + processor.CapitalizeWords());

        Console.WriteLine("单词反转:" + processor.ReverseWords());
    }
}

public class StringProcessor
{
    private string _input;

    public StringProcessor(string input)
    {
        _input = input;
    }

    // 统计字符串中单词的数量
    public int CountWords()
    {
        int count = 0;
        bool inWord = false;

        foreach (char c in _input)
        {
            if (char.IsWhiteSpace(c))
            {
                inWord = false;
            }
            else
            {
                if (!inWord)
                {
                    count++;
                    inWord = true;
                }
            }
        }

        return count;
    }

    // 查找字符串中最长的单词,并显示其长度
    public (string word, int length) FindLongestWord()
    {
        int maxLength = 0;
        string longestWord = "";
        string currentWord = "";

        foreach (char c in _input)
        {
            if (char.IsWhiteSpace(c))
            {
                if (currentWord.Length > maxLength)
                {
                    maxLength = currentWord.Length;
                    longestWord = currentWord;
                }
                currentWord = "";
            }
            else
            {
                currentWord += c;
            }
        }

        if (currentWord.Length > maxLength)
        {
            maxLength = currentWord.Length;
            longestWord = currentWord;
        }

        return (longestWord, maxLength);
    }

    // 将字符串中的所有单词首字母大写
    public string CapitalizeWords()
    {
        char[] result = new char[_input.Length];
        bool newWord = true;

        for (int i = 0; i < _input.Length; i++)
        {
            if (char.IsWhiteSpace(_input[i]))
            {
                result[i] = _input[i];
                newWord = true;
            }
            else
            {
                if (newWord && char.IsLetter(_input[i]))
                {
                    result[i] = char.ToUpper(_input[i]);
                    newWord = false;
                }
                else
                {
                    result[i] = _input[i];
                }
            }
        }

        return new string(result);
    }

    // 将字符串中的所有单词反转
    public string ReverseWords()
    {
        char[] result = new char[_input.Length];
        int start = 0, end = 0;

        while (start < _input.Length)
        {
            while (end < _input.Length && !char.IsWhiteSpace(_input[end]))
            {
                end++;
            }

            int wordEnd = end - 1;
            for (int i = start; i < end; i++)
            {
                result[i] = _input[wordEnd--];
            }

            while (end < _input.Length && char.IsWhiteSpace(_input[end]))
            {
                result[end] = _input[end];
                end++;
            }

            start = end;
        }

        return new string(result);
    }
}

运行结果:

 

实验小结

通过本次实验,我们成功实现了一个字符串处理器,并巩固了以下知识和技能:

  1. 面向对象编程:掌握了如何定义类和方法,如何创建对象,以及如何通过对象调用方法。
  2. 字符串处理:学习了如何手动实现基本的字符串操作,如统计单词数量、查找最长单词、首字母大写和单词反转。
  3. C# 编程:熟悉了 C# 的基本语法和控制台应用程序的开发流程。

重难点分析

  1. 字符串遍历与处理:在不使用现成的字符串处理函数的情况下,手动实现字符串操作需要对字符串的遍历和字符处理有深入理解。特别是在处理单词的边界和处理连续空白字符时,需要仔细考虑各种情况。
  2. 单词反转的实现:在反转单词时,需要正确识别单词的起始和结束位置,并正确地反转字符顺序。这需要对字符串索引和字符操作非常熟悉。
  3. 代码鲁棒性:处理不同的输入情况,如多个连续空格、空字符串等,需要确保代码的健壮性,避免出现意外错误。
  • 12
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值