Found 7346 Articles for C++

C++ program to Convert a Decimal Number to Binary Number using Stacks

Jennifer Nicholas
Updated on 30-Jul-2019 22:30:25

3K+ Views

In this problem, we will see how to convert a decimal number to binary numbers using stacks. As we know that the decimal numbers can be converted using binary after dividing it by 2 and taking the remainder. We take the remainder from last to first, so we can easily use the stack data structure to do that.Input: Decimal number 13 Output: Binary number 1101AlgorithmStep 1: Take a number in decimal Step 2: while the number is greater than 0: Step 2.1: Push the remainder after dividing the number by 2 into stack. Step 2.2: set the number as number ... Read More

C++ Program to Check for balanced paranthesis by using Stacks

Jennifer Nicholas
Updated on 30-Jul-2019 22:30:25

8K+ Views

Here we will discuss how to check the balanced brackets using stacks. We not only check the opening and closing brackets but also check the ordering of brackets. For an example we can say that the expression "[{} () {()}]" it is correct, but "{[}]" it is not correct.Input: Some expression with brackets "{()}[]" Output: They are balancedAlgorithmStep 1: Define a stack to hold brackets Step 2: Traverse the expression from left to right Step 2.1: If the character is opening bracket (, or { or [, then push it into stack Step 2.2: If the character is closing bracket ... Read More

Replace substring with another substring C++

Nitya Raut
Updated on 30-Jul-2019 22:30:25

2K+ Views

Here we will see how to replace substring with another substring. It replaces the portion of the string that begins at character pos and spans len characters.The structure of the replace function is like below:string& replace (size_t pos,  size_t len,  const string& str,  size_t subpos,  size_t sublen);The parameters are pos: It is an insertion point, str : It is a string object, len : It contains information about number of characters to erase.AlgorithmStep 1: Get the main string, and the string which will be replaced. And the match string Step 2: While the match string is present in the main string: Step 2.1: Replace it with the given ... Read More

Replace part of a string with another string in C++

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:25

5K+ Views

Here we will see how to replace a part of a string by another string in C++. In C++ the replacing is very easy. There is a function called string.replace(). This replace function replaces only the first occurrence of the match. To do it for all we have used loop. This replace function takes the index from where it will replace, it takes the length of the string, and the string which will be placed in the place of matched string.Input: A string "Hello...Here all Hello will be replaced", and another string to replace "ABCDE" Output: "ABCDE...Here all ABCDE will ... Read More

Remove Trailing Zeros from string in C++

Jennifer Nicholas
Updated on 30-Jul-2019 22:30:25

3K+ Views

IN this program we will see how to remove the trailing zeros from a string in C++. Sometimes some string may contain trailing zeros like "00023054". After executing this program, it will return "23054" only. The initial zeros are removed.Input: A string with trailing zeros “000023500124” Output: “23500124”AlgorithmStep 1: Get the string Step 2: Count number of trailing zeros n Step 3: Remove n characters from the beginning Step 4: return remaining string.Example Code Live Demo#include using namespace std; main() {    string my_str = "000023500124";    int num = 0;    cout

Remove spaces from std::string in C++

Nitya Raut
Updated on 30-Jul-2019 22:30:25

14K+ Views

In this program we will see how to remove the spaces from a std::string in C++. To remove this we will use the remove() function. With this remove() function it takes the beginning and end of the iterator, then takes the third argument that will be deleted from that iterator object.Input: A string "This is C++ Programming Language" Output: "ThisisC++ProgrammingLanguage"AlgorithmStep 1: Get the string Step 2: Remove spaces from the given string using remove() function. Step 3: Return string.Example Code Live Demo#include #include using namespace std; main() {    string my_str = "This is C++ Programming Language";    cout

Raw string literal in C++

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:25

400 Views

In C++11 and above there is a concept called Raw string. In strings we use different characters like , \t etc. They have different meaning. The is used to return the cursor to the next line, the \t generates a tab etc.If we want to print these characters in the output without seeing the effect of them, we can use the raw string mode. To make a string to raw string we have to add "R" before the string.Input: A string "Hello\tWorldC++" Output: "Hello\tWorldC++"AlgorithmStep 1: Get the string Step 2: Use R before string to make it raw string ... Read More

Program to loop on every character in string in C++

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:25

12K+ Views

Here in this program we will see how to iterate through each characters of a string in C++. To loop on each character, we can use loops starting from 0 to (string length – 1). For accessing the character we can either use subscript operator "[ ]" or at() function of string object.Input: A string “Hello World” Output: “Hello World”AlgorithmStep 1: Get the string Step 2: Use R before string to make it raw string Step 3: EndExample Code Live Demo#include using namespace std; main() { string my_str = "Hello World"; for(int i = 0; i

Parsing a comma-delimited std::string in C++

Nitya Raut
Updated on 30-Jul-2019 22:30:25

6K+ Views

In this program we will see how to parse comma-delimited string in C++. We will put a string where some texts are present, and they are delimited by comma. After executing this program, it will split those strings into a vector type object.To split them we are using the getline() function. The basic syntax of this function is like:getline (input_stream, string, delim)This function is used to read a string or a line from input stream.Input: Some strings "ABC, XYZ, Hello, World, 25, C++" Output: Separated string ABC XYZ Hello World 25 C++AlgorithmStep 1: Create stream from given string Step 2: ... Read More

How to concatenate multiple C++ strings on one line?

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:25

841 Views

Here we will see how to concatenate multiple strings in one line in C++. There are few different methods to do that. The easiest method is by using the plus (+) operator. The strings can be concatenated using +. We can place + sign between two strings to make them concatenated.Input: Some strings “str1”, “str2”, “str3” Output: Concatenated string “str1str2str3”AlgorithmStep 1: Take some strings Step 2: Concatenate them by placing + sign between them Step 3: Display the string Step 4: EndExample Code Live Demo#include using namespace std; int main() {    string str1, str2, str3;    str1 = "Hello"; ... Read More

Advertisements