【C#】字符串处理器

ops/2024/10/18 18:27:00/

实现:

  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. 代码鲁棒性:处理不同的输入情况,如多个连续空格、空字符串等,需要确保代码的健壮性,避免出现意外错误。

http://www.ppmy.cn/ops/52273.html

相关文章

(一)SvelteKit教程:hello world

&#xff08;一&#xff09;SvelteKit教程&#xff1a;hello world sveltekit 的官方教程&#xff0c;在这里&#xff1a;Creating a project • Docs • SvelteKitCreating a project • Docs • SvelteKit 我们可以按照如下的步骤来创建一个项目&#xff1a; npm create s…

栈(数据结构篇)

数据结构篇之栈 栈(stack) 概念 栈(stack)是限制插入和删除只能在一个位置上的进行的表&#xff0c;而这个模型唯一的开口位置是表的末端&#xff0c;叫做栈顶(top)&#xff0c;相反表的首部&#xff0c;叫做栈底。对于栈的基本操作有Push(进栈)和Pop(出栈)&#xff0c;前者…

计算机组成原理-第6章计算机的运算方法

6.1无符号数和有符号数 6.1.1无符号数 没有符号&#xff0c;在寄存器中的每一位均可用来存放数值。 6.1.2有符号数 1&#xff0c;机器数与真值&#xff1a;0表示正&#xff0c;1表示负。 把符号数字化的数称为机器数&#xff0c;而把带或-符号的数称为真值。 2&#xff0…

抽卡机小程序:设计与开发全攻略

在移动互联网时代&#xff0c;小程序以其轻便、易用、无需安装的特点&#xff0c;迅速成为用户日常使用的重要工具。其中&#xff0c;抽卡机小程序因其独特的娱乐性和互动性&#xff0c;受到广大用户的喜爱。本文将为大家详细介绍抽卡机小程序的设计与开发全攻略。 一、需求分析…

ARM功耗管理软件之软件栈及示例

安全之安全(security)博客目录导读 思考&#xff1a;功耗管理软件栈及示例&#xff1f;WFI&WFE&#xff1f;时钟&电源树&#xff1f;DVFS&AVS&#xff1f; SoC底层软件低功耗系统设计与实现-CSDN直播 ARM功耗管理精讲与实战汇总参见&#xff1a;Arm功耗管理精讲与…

越复杂的CoT越有效吗?Complexity-Based Prompting for Multi-step Reasoning

Complexity-Based Prompting for Multi-step Reasoning 论文&#xff1a;https://openreview.net/pdf?idyf1icZHC-l9 Github&#xff1a;https://github.com/FranxYao/chain-of-thought-hub 发表位置&#xff1a;ICLR 2023 Complexity-Based Prompting for Multi-step Reason…

前端面试题(基础篇七)

一、谈谈你对webpack的看法 webpack是一个模块打包工具&#xff0c;我们可以使用webpack管理我们的模块依赖&#xff0c;编译输出模块所需的静态文件。它可以很好的管理、打包web开发中所需的html、css、JavaScript以及其他各种静态文件&#xff08;使用的图片、字体图标等&am…

23.并发

目录 一、一些概念二、进程和线程2.1 概念2.2 多线程导致的问题2.3 使用spawn创建新线程2.4 线程与move闭包 三、消息传递3.1 概念3.2 创建通道3.3 示例3.4 其它测试 四、共享状态并发4.1 互斥器4.2 Mutex的API4.3 多线程共享Mutex1&#xff09;在多线程之间共享值&#xff1b;…