Found 7346 Articles for C++

C++ Program to Perform Dictionary Operations in a Binary Search Tree

George John
Updated on 30-Jul-2019 22:30:25

1K+ Views

A Binary Search Tree is a sorted binary tree in which all the nodes have following two properties−The right sub-tree of a node has a key greater than to its parent node's key.The left sub-tree of a node has a key less than or equal to its parent node's key.Each node should not have more than two children.This is a C++ Program to Perform Dictionary Operations in a Binary Search Tree.AlgorithmFor insert:Begin    Declare function insert(int k)       in = int(k mod max)       p[in] = (n_type*) malloc(sizeof(n_type))       p[in]->d = k     ... Read More

C++ Program to Implement Ternary Tree

Chandu yadav
Updated on 30-Jul-2019 22:30:25

414 Views

A ternary tree, is a tree data structure in which each node has at most three child nodes, usually represented as “left”, “mid” and “right”. In this tree, nodes with children are parent nodes, and child nodes may contain references to their parents. This is a C++ Program to Implement Ternary Tree and traversal of the tree.AlgorithmBegin    Declare function insert(struct nod** root, char *w)       if (!(*root)) then          *root = newnod(*w);          if ((*w) < (*root)->d) then             insert(&( (*root)->l ), w);       ... Read More

What is the easiest way to convert int to string in C++

Smita Kapse
Updated on 30-Jul-2019 22:30:25

325 Views

In this section, we will see how to convert an integer to a string.The logic is very simple. Here we will use the sprintf() function. This function is used to print some value or line into a string, but not in the console. This is the only difference between printf() and sprintf(). Here the first argument is the string buffer. where we want to save our data.Input: User will put some numeric value say 42 Output: This program will return the string equivalent result of that number like “42”AlgorithmStep 1: Take a number from the user Step 2: Create an ... Read More

Tokenizing a string in C++

Nishtha Thakur
Updated on 30-Jul-2019 22:30:25

5K+ Views

In this section, we will see how to tokenize strings in C++. In C we can use the strtok() function for the character array. Here we have a string class. Now we will see how to cut the string using some delimiter from that string.To use the C++ feature, we have to convert a string to a string stream. Then using getline() function we can do the task. The getline() function takes the string stream, another string to send the output, and the delimiter to stop the stream from scanning.Let us see the following example to understand how the function ... Read More

The most elegant way to iterate the words of a string using C++

Anvi Jain
Updated on 30-Jul-2019 22:30:25

100 Views

There is no one elegant way to iterate the words of a C/C++ string. The most readable way could be termed as the most elegant for some while the most performant for others. I've listed 2 methods that you can use to achieve this. First way is using a stringstream to read words separated by spaces. This is a little limited but does the task fairly well if you provide the proper checks. For example, >Example Code Live Demo#include #include #include #include using namespace std; int main() {    string str("Hello from the dark side");   ... Read More

string at() function in C++

Nishtha Thakur
Updated on 30-Jul-2019 22:30:25

84 Views

In this section, we will see what is the at a () function in C++. The at() function is used to access the character at a given position.In this program, we will iterate through each character using at a () function and print them into different lines.Example Code Live Demo#include using namespace std; main() {    string my_str = "Hello World";    for(int i = 0; i

How to remove certain characters from a string in C++?

Anvi Jain
Updated on 02-Sep-2023 10:50:28

77K+ Views

In this section, we will see how to remove some characters from a string in C++. In C++ we can do this task very easily using erase() and remove() function. The remove function takes the starting and ending address of the string, and a character that will be removed.Input: A number string “ABAABACCABA” Output: “BBCCB”AlgorithmStep 1:Take a string Step 2: Remove each occurrence of a specific character using remove() function Step 3: Print the result. Step 4: EndExample Code Live Demo#include #include using namespace std; main() {    string my_str = "ABAABACCABA";    cout

How to quickly reverse a string in C++?

Smita Kapse
Updated on 30-Jul-2019 22:30:25

187 Views

In this section, we will see how to reverse a string very quickly using C++. For reversing there is a built-in function in the algorithm library, called reverse(). This function takes the beginning and the ending pointer of a container, then reverse the elements.Input: A number string “Hello World” Output: “dlroW olleH”AlgorithmStep 1:Take a string Step 2: reverse it using reverse() function Step 3: Print the result. Step 4: EndExample Code Live Demo#include #include using namespace std; main() {    string my_str = "This is a string to be reversed";    cout

How to map C++ enums to strings?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:25

3K+ Views

Here we will see how to map some enum type data to a string in C++. There is no such direct function to do so. But we can create our own function to convert enum to string.We shall create a function that takes an enum value as the argument, and we manually return the enum names as a string from that function.Example Code Live Demo#include using namespace std; enum Animal {Tiger, Elephant, Bat, Dog, Cat, Mouse}; string enum_to_string(Animal type) {    switch(type) {       case Tiger:          return "Tiger";       case ... Read More

How to convert std::string to lower case in C++?

Anvi Jain
Updated on 30-Jul-2019 22:30:25

8K+ Views

In this section, we will see how to convert all letters of a C++ string to lowercase letters. To do this thing we have to use the transform function. This transform function is present in the algorithm library.The transform function takes the beginning pointer of the string and the ending pointer of the string. It also takes the beginning of the string to store the result, then the fourth argument is ::tolower. This helps to convert the string into a lowercase string. We can use this same method if we want to convert some string into an uppercase string.Example Code Live ... Read More

Advertisements