0

0

根据单词数量反转字符串

WBOY

WBOY

发布时间:2023-09-03 15:09:06

|

973人浏览过

|

来源于tutorialspoint

转载

根据单词数量反转字符串

String manipulation is an essential skill in programming, as it helps us process and analyze text data efficiently. C++ provides a rich set of string manipulation functions and objects, making it easier to work with text data.

In this article, we will discuss how to reverse a string according to the number of words in C++.

方法

Approach 1 − Using stringstreams and vectors

方法2 - 使用子字符串和字符串操作函数

Syntax

在C++中的字符串对象:std::string类是C++标准库的一部分,提供了各种字符串操作函数。

String manipulation functions: Some common string manipulation functions in C++ include length(), substr(), find(), erase(), and replace().

std::string reverseStringByWords(const std::string& input) {}
std::reverse(words.begin(), words.end());

Approach 1:- Using stringstreams and vectors

This approach employed in the code involves converting the input string into a sequence of words using a stringstream object. The words are then extracted one by one from the stream and stored in a collection of strings represented by a vector.

Subsequently, the collection of words is reversed using the reverse function from the algorithm library. The reversed words are then joined together to form the final output string, with a space appended after each word except the last one.

算法

  • 开始

  • 获取输入字符串。

  • 从输入字符串创建一个stringstream。

  • 初始化一个空向量来存储单词。

  • 通过stringstream迭代提取单词。

  • Extract a word from the stringstream.

  • Push the extracted word into the vector.

  • Reverse the vector containing the words.

  • 初始化一个空的输出字符串。

  • 通过反向遍历向量形成输出字符串。

  • Add each word from the reversed vector to the output string, followed by a space.

  • Remove the last space from the output string.

  • 返回输出字符串。

    Android数据格式解析对象JSON用法 WORD版
    Android数据格式解析对象JSON用法 WORD版

    本文档主要讲述的是Android数据格式解析对象JSON用法;JSON可以将Java对象转成json格式的字符串,可以将json字符串转换成Java。比XML更轻量级,Json使用起来比较轻便和简单。JSON数据格式,在Android中被广泛运用于客户端和服务器通信,在网络数据传输与解析时非常方便。希望本文档会给有需要的朋友带来帮助;感兴趣的朋友可以过来看看

    下载
  • End

Example

The code embodies a procedure that inverts the sequence of words in a specified string. This is accomplished by first transforming the input string into a word-based stream through utilization of a stringstream object. Subsequently, the words are extracted one at a time and placed into a vector. Then, the reverse function from the algorithm library is employed to reverse the vector. Finally, the inverted words are joined together to form the conclusive output string, with spaces inserted after each word, excluding the final one. The last space is then deleted from the output string, rendering a compact and comprehensible solution that makes the most of Standard Library features like stringstreams, vectors, and the algorithm library.

#include 
#include 
#include 
#include 
#include 

std::string reverseStringByWords(const std::string& input) {
   std::stringstream ss(input);
   std::string word;
   std::vector words;

   while (ss >> word) {
      words.push_back(word);
   }

   std::reverse(words.begin(), words.end());

   std::string output;
   for (const auto& w : words) {
      output += w + " ";
   }

   output.pop_back(); // Remove the last space
   return output;
}

int main() {
   std::string input = "Hello, how are you?";
   std::string output = reverseStringByWords(input);
   std::cout << "Input: " << input << std :: endl;
   std:: cout << "Output: " << output << std :: endl;
   return 0;
}

Output

的中文翻译为:

输出

Input: Hello, how are you?
Output: you? are how Hello,

方法2:使用子字符串和字符串操作函数

方法2是将字符串中的单词顺序颠倒的另一种解决方案。它使用子字符串和字符串操作函数,而不是像方法1中使用字符串流和向量。

This approach involves manually dividing the input string into substrings, which represent individual words. The substrings are concatenated in reverse order to form the final output string.

算法

  • 开始

  • 获取输入字符串。

  • 初始化两个 size_t 变量 start 和 end,用于保存输入字符串中单词的起始和结束位置。

  • 将起始位置初始化为0。

  • 找到输入字符串中第一个空格的位置,并将其存储在end变量中。

  • 初始化一个空的输出字符串。

  • Iterate through the input string and extract words using substrings.

  • 从起始位置提取子字符串到结束位置。

  • Concatenate the extracted substring in front of the output string, followed by a space.

  • Update the start position to the position after the end position.

  • Find the next space in the input string starting from the new start position and update the end position.

  • 循环结束后,使用

  • End

Example

The code is a solution for reversing the order of words in a given string. It does this by dividing the input string into substrings using the find function and concatenating these substrings in reverse order to form the output string. The last space character is then removed from the output string using the pop_back function. This approach is more manual and low-level compared to Approach 1 and requires a deeper understanding of string manipulation. The code takes a given input string, divides it into substrings, reverses the order of these substrings, and returns the final output string.

#include 
#include 

std::string reverseStringByWords(const std::string& input) {
   size_t start = 0;
   size_t end = input.find(' ');
   std::string output;

   while (end != std::string::npos) {
      output = input.substr(start, end - start) + " " + output;
      start = end + 1;
      end = input.find(' ', start);
   }
    
   output = input.substr(start) + " " + output;
   output.pop_back(); // Remove the last space
   return output;
}

int main() {
   std::string input = "Hello, how are you?";
   std::string output = reverseStringByWords(input);
   std::cout << "Input: " << input << std::endl;
   std::cout << "Output: " << output << std::endl;

   return 0;
}

Output

的中文翻译为:

输出

Input: Hello, how are you?
Output: you? are how Hello,

结论

在编程中字符串操作的重要性:掌握字符串操作技术对于任何程序员来说都是至关重要的,因为文本数据在软件开发中无处不在。理解各种字符串操作的方法可以帮助开发人员编写更高效、可维护和健壮的代码。

相关专题

更多
php源码安装教程大全
php源码安装教程大全

本专题整合了php源码安装教程,阅读专题下面的文章了解更多详细内容。

7

2025.12.31

php网站源码教程大全
php网站源码教程大全

本专题整合了php网站源码相关教程,阅读专题下面的文章了解更多详细内容。

4

2025.12.31

视频文件格式
视频文件格式

本专题整合了视频文件格式相关内容,阅读专题下面的文章了解更多详细内容。

7

2025.12.31

不受国内限制的浏览器大全
不受国内限制的浏览器大全

想找真正自由、无限制的上网体验?本合集精选2025年最开放、隐私强、访问无阻的浏览器App,涵盖Tor、Brave、Via、X浏览器、Mullvad等高自由度工具。支持自定义搜索引擎、广告拦截、隐身模式及全球网站无障碍访问,部分更具备防追踪、去谷歌化、双内核切换等高级功能。无论日常浏览、隐私保护还是突破地域限制,总有一款适合你!

7

2025.12.31

出现404解决方法大全
出现404解决方法大全

本专题整合了404错误解决方法大全,阅读专题下面的文章了解更多详细内容。

42

2025.12.31

html5怎么播放视频
html5怎么播放视频

想让网页流畅播放视频?本合集详解HTML5视频播放核心方法!涵盖<video>标签基础用法、多格式兼容(MP4/WebM/OGV)、自定义播放控件、响应式适配及常见浏览器兼容问题解决方案。无需插件,纯前端实现高清视频嵌入,助你快速打造现代化网页视频体验。

4

2025.12.31

关闭win10系统自动更新教程大全
关闭win10系统自动更新教程大全

本专题整合了关闭win10系统自动更新教程大全,阅读专题下面的文章了解更多详细内容。

3

2025.12.31

阻止电脑自动安装软件教程
阻止电脑自动安装软件教程

本专题整合了阻止电脑自动安装软件教程,阅读专题下面的文章了解更多详细教程。

3

2025.12.31

html5怎么使用
html5怎么使用

想快速上手HTML5开发?本合集为你整理最实用的HTML5使用指南!涵盖HTML5基础语法、主流框架(如Bootstrap、Vue、React)集成方法,以及无需安装、直接在线编辑运行的平台推荐(如CodePen、JSFiddle)。无论你是新手还是进阶开发者,都能轻松掌握HTML5网页制作、响应式布局与交互功能开发,零配置开启高效前端编程之旅!

2

2025.12.31

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号