

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 space with a hyphen in C++
In this C++ program, the space in the string will be replaced with the hyphen. Firstly, the length of the string is determined by the length() function of the cstring class, then hyphen is filled into the space of the sentence by traversing the string as follows.
Example
#include <cstring> #include <iostream> using namespace std; int main(){ // raw string declaration string str = "Coding in C++ programming"; cout<<"Normal String::"<<str<<endl; for (int i = 0; i < str.length(); ++i) { // replacing character to '-' with a 'space'. if (str[i] == ' ') { str[i] = '-'; } } // output string with '-'. cout <<"Output string::"<< str << endl; return 0; }
Output
The output of the program is yield with hyphen tweak as following when the user enters the string as following;
Normal String::Coding in C++ programming Output string::Coding-in-C++-programming
- Related Questions & Answers
- Python Program to Take in a String and Replace Every Blank Space with Hyphen
- PHP preg_split with hyphen?
- Replacing words with asterisks in C++
- Replacing spaces with underscores in JavaScript?
- Replacing dots with dashes in a string using JavaScript
- Replacing zero starting with whitespace in JavaScript
- Replacing numbers on a comma delimited result with MySQL?
- Replacing Substrings in a Java String
- MySQL left padding with zeros separated by hyphen?
- How to set a string with hyphen and numbers in MySQL varchar?
- JavaScript: replacing object keys with an array
- Replacing vowels with their 1-based index in a string in JavaScript
- How to concatenate string vectors separated with hyphen in R?
- MySQL query to remove numbers after hyphen in a VARCHAR string with numbers
- Replacing/Installing a license in SAP HANA
Advertisements