在本文中,我们将解决从给定字符串中删除给定单词的问题。例如 -
Input : str = “remove a given word ”, word = “ remove ” Output : “ a given word ” Input : str = “ god is everywhere ”, word = “ is ” Output : “ god everywhere ”
寻找解决方案的方法
例如,我们可以使用一种简单的方法从字符串中删除单词。
- 首先,将二维矩阵形式的给定字符串,其中每个单词存储在每一行中。
- 在矩阵中查找该单词,并将该行替换为该单词所在的空字符。
- 最后,打印重新排序的字符串。
示例
#include <bits/stdc++.h> using namespace std; int remove_word (string str, char word[]) { char matrix[10][30]; int i = 0, j = 0, k = 0, len1 = 0, len2 = 0; // putting each word of string into the rows of the 2-D matrix. for (i = 0; str[i] != '\0'; i++) { if (str[i] == ' ') { matrix[k][j] = '\0'; k++; j = 0; }else{ matrix[k][j] = str[i]; j++; } } // looking for the word in a given string and putting a null character when the word is found. matrix[k][j] = '\0'; j = 0; for (i = 0; i < k + 1; i++) { if (strcmp (matrix[i], word) == 0) { matrix[i][j] = '\0'; } } j = 0; // printing the reordered string. for (i = 0; i < k + 1; i++){ if (matrix[i][j] == '\0') continue; else cout << matrix[i] << " "; } cout << "\n"; } int main () { char str1[] = "remove a given word", word1[] = "remove"; char str2[] = "god is everywhere", word2[]="is"; // calling a function to remove a word from a string and print it. remove_word (str1, word1); remove_word (str2, word2); return 0; }
输出
a given word god everywhere
上述代码的解释
- 用一些值初始化字符串和数组,并调用函数来删除给定的单词。
- 将每个单词放入二维矩阵的每一行中的字符串,使用循环存储每个块中的每个字符,直到找到空格。
- 使用 strcmp() 函数将字符串与单词进行比较,并将 null 值放入其中的行中找到单词。
- 最后,通过打印矩阵的每一行来打印字符串。
结论
本文讨论了删除给定单词来自字符串,我们通过将字符串存储在二维矩阵中然后用空值替换该单词来解决问题。我们使用 C++ 代码解决了这个问题。但是,我们可以使用任何其他语言(例如 C、Java、Python 等)解决相同的问题。希望本文对您有所帮助。
The above is the detailed content of Remove a given word from a string using C++. For more information, please follow other related articles on the PHP Chinese website!

The history and evolution of C# and C are unique, and the future prospects are also different. 1.C was invented by BjarneStroustrup in 1983 to introduce object-oriented programming into the C language. Its evolution process includes multiple standardizations, such as C 11 introducing auto keywords and lambda expressions, C 20 introducing concepts and coroutines, and will focus on performance and system-level programming in the future. 2.C# was released by Microsoft in 2000. Combining the advantages of C and Java, its evolution focuses on simplicity and productivity. For example, C#2.0 introduced generics and C#5.0 introduced asynchronous programming, which will focus on developers' productivity and cloud computing in the future.

There are significant differences in the learning curves of C# and C and developer experience. 1) The learning curve of C# is relatively flat and is suitable for rapid development and enterprise-level applications. 2) The learning curve of C is steep and is suitable for high-performance and low-level control scenarios.

There are significant differences in how C# and C implement and features in object-oriented programming (OOP). 1) The class definition and syntax of C# are more concise and support advanced features such as LINQ. 2) C provides finer granular control, suitable for system programming and high performance needs. Both have their own advantages, and the choice should be based on the specific application scenario.

Converting from XML to C and performing data operations can be achieved through the following steps: 1) parsing XML files using tinyxml2 library, 2) mapping data into C's data structure, 3) using C standard library such as std::vector for data operations. Through these steps, data converted from XML can be processed and manipulated efficiently.

C# uses automatic garbage collection mechanism, while C uses manual memory management. 1. C#'s garbage collector automatically manages memory to reduce the risk of memory leakage, but may lead to performance degradation. 2.C provides flexible memory control, suitable for applications that require fine management, but should be handled with caution to avoid memory leakage.

C still has important relevance in modern programming. 1) High performance and direct hardware operation capabilities make it the first choice in the fields of game development, embedded systems and high-performance computing. 2) Rich programming paradigms and modern features such as smart pointers and template programming enhance its flexibility and efficiency. Although the learning curve is steep, its powerful capabilities make it still important in today's programming ecosystem.

C Learners and developers can get resources and support from StackOverflow, Reddit's r/cpp community, Coursera and edX courses, open source projects on GitHub, professional consulting services, and CppCon. 1. StackOverflow provides answers to technical questions; 2. Reddit's r/cpp community shares the latest news; 3. Coursera and edX provide formal C courses; 4. Open source projects on GitHub such as LLVM and Boost improve skills; 5. Professional consulting services such as JetBrains and Perforce provide technical support; 6. CppCon and other conferences help careers

C# is suitable for projects that require high development efficiency and cross-platform support, while C is suitable for applications that require high performance and underlying control. 1) C# simplifies development, provides garbage collection and rich class libraries, suitable for enterprise-level applications. 2)C allows direct memory operation, suitable for game development and high-performance computing.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

WebStorm Mac version
Useful JavaScript development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.