【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);
}
  1. 将字符串中的所有单词反转。

    // 将字符串中的所有单词反转
    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. 代码鲁棒性:处理不同的输入情况,如多个连续空格、空字符串等,需要确保代码的健壮性,避免出现意外错误。
相关推荐
想不明白的过度思考者几秒前
Java从入门到“放弃”(精通)之旅——JavaSE终篇(异常)
java·开发语言
我真的不会C20 分钟前
QT窗口相关控件及其属性
开发语言·qt
CodeCraft Studio21 分钟前
Excel处理控件Aspose.Cells教程:使用 Python 在 Excel 中进行数据验
开发语言·python·excel
火柴盒zhang26 分钟前
websheet之 编辑器
开发语言·前端·javascript·编辑器·spreadsheet·websheet
景天科技苑33 分钟前
【Rust】Rust中的枚举与模式匹配,原理解析与应用实战
开发语言·后端·rust·match·enum·枚举与模式匹配·rust枚举与模式匹配
阿让啊39 分钟前
C语言中操作字节的某一位
c语言·开发语言·数据结构·单片机·算法
椰羊~王小美44 分钟前
LeetCode -- Flora -- edit 2025-04-25
java·开发语言
孞㐑¥1 小时前
C++11介绍
开发语言·c++·经验分享·笔记
旦莫1 小时前
Python 教程:我们可以给 Python 文件起中文名吗?
开发语言·python
꧁坚持很酷꧂2 小时前
配置Ubuntu18.04中的Qt Creator为中文(图文详解)
开发语言·qt·ubuntu