- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Replacing words with asterisks in C++
This aim of this program to replace a particular word with asterisks in the string by using the c++ programming code. The essential function of vector and string class essay a key role to achieve the prospective results. The algorithm is as follows;
Algorithm
START Step-1: Input string Step-2 Split the string into words and store in array list Step-3: Iterate the loop till the length and put the asterisk into a variable Step-4: Traverse the array foreah loop and compare the string with the replaced word Step-5: Print END
Now, the following code is carved out based on the algorithm. Here, the strtok() method of string split the string and returns the array list type of vector class.
Example
#include <cstring> #include <iostream> #include <vector> #include <string.h> //method for spliting string std::vector<std::string> split(std::string str,std::string sep){ char* cstr=const_cast<char*>(str.c_str()); char* current; std::vector<std::string> arr; current=strtok(cstr,sep.c_str()); while(current!=NULL){ arr.push_back(current); current=strtok(NULL,sep.c_str()); } return arr; } int main(){ std::vector<std::string> word_list; std::cout<<"string before replace::"<<"Hello ajay yadav ! you ajay you are star"<<std::endl; std::cout<<"word to be replaced::"<<"ajay"<<std::endl; word_list=split("Hello ajay yadav ! you ajay you are star"," "); std::string result = ""; // Creating the censor which is an asterisks // "*" text of the length of censor word std::string stars = ""; std::string word = "ajay"; for (int i = 0; i < word.length(); i++) stars += '*'; // Iterating through our list // of extracted words int index = 0; for (std::string i : word_list){ if (i.compare(word) == 0) // changing the censored word to // created asterisks censor word_list[index] = stars; index++; } // join the words for (std::string i : word_list){ result += i + ' '; } std::cout<<"output::"<<result; return 0; } }
As seen in the above code, all string operation code is bundled into the retrieveChar() method, later on, which call is passed to program main() execution.
Output
String before replace::Hello ajay yadav ! you ajay you are star Word to be replaced::ajay Output::Hello **** yadav ! you **** you are star
- Related Articles
- Replacing space with a hyphen in C++
- C++ Program to find matrix with marked asterisks region
- C# Program to replace a character with asterisks in a sentence
- C Program for replacing one digit with other
- Replacing spaces with underscores in JavaScript?
- Usage of Asterisks in Python
- Replacing zero starting with whitespace in JavaScript
- Python Program to replace a word with asterisks in a sentence
- Java Program to replace a word with asterisks in a sentence
- Replacing ‘public’ with ‘private’ in “main” in Java
- Substring with Concatenation of All Words in C++
- JavaScript: replacing object keys with an array
- Replacing strings with numbers in Python for Data Analysis
- Replacing dots with dashes in a string using JavaScript
- Substring with Concatenation of All Words in C++ Program

Advertisements