Found 7346 Articles for C++

How to concatenate a std::string and an int in C++?

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

2K+ Views

In this program we will see how to concatenate a string and integer type data in C++. To concatenate string and integer data, we have to convert integer to string at first. To convert it we are using stringstream. This provides some features. It takes number or string then make it string.Input: String “str” and Number 10 Output: Concatenated string “str10”AlgorithmStep 1: Take string and number Step 2: Convert number to string Step 3: Concatenate them Step 4: EndExample Code Live Demo#include #include using namespace std; string int_to_str(int x) {    stringstream ss;    ss

How do you reverse a string in place in C or C++?

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

478 Views

In this section we will see how to reverse a string in place. So we will not use some other memory spaces for reversing. In C++, we can use the std::string. But for C we have to use the character array. In this program we are using the character array to take the string. Then reversing it.Input: A string “This is a string” Output: The reversed string “gnirts a si sihT”Algorithmreverse_string(str)Input − The stringOutput − The reversed string.len := the length of the string i := 0 and j := (len-1) while i < j, do swap ... Read More

How do I create a random alpha-numeric string using C++?

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

721 Views

In this section we will see how to generate a random alphanumeric string using C++. Here we are providing lowercase letters, uppercase letters and numbers (0-9). This program takes the characters randomly, then creates the random string.Input: Here we are giving the string length Output: A random string of that length. Example “XSme6VAsvJ”AlgorithmStep 1:Define array to hold all uppercase, lowercase letters and numbers Step 2: Take length n from user Step 3: Randomly choose characters’ n times and create a string of length n Step 4: EndExample Code Live Demo#include #include #include #include using namespace std; static ... Read More

Extract all integers from string in C++

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

5K+ Views

Here we will see how to extract all integers from strings in C++. We can put a string where numbers and no-numbers are present. We will extract all numeric values from it.To solve this problem, we will use the stringstream class in C++. We will cut the string word by word and then try to convert it into integer type data. if the conversion is done, then it is integer and print the value.Input: A string with some numbers “Hello 112 World 35 75” Output: 112 35 75AlgorithmStep 1:Take a number string Step 2: Divide it into different words Step ... Read More

Converting string to number and vice-versa in C++

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

607 Views

In this section we will see how to convert string to number and number to string. At first we will see how to convert string to number.String to Number ConversionHere we will see how to convert a number string to integer type data. We can solve this problem by using the atoi() function. This function takes string as input and converts into integer data.The atoi() function is present in the library.Input: A number string “1234” Output: 1234AlgorithmStep 1:Take a number string Step 2: Convert it to integer using atoi() function Step 3: Print the result. Step 4: EndExample Code Live ... Read More

Convert number string to integers in C++

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

235 Views

Here we will see how to convert a number string to integer type data. We can solve this problem by using the atoi() function. This function takes string as input and converts into integer data.The atoi() function is present in the library.Input: A number string “1234” Output: 1234AlgorithmStep 1: Take a number string Step 2: Convert it to integer using atoi() function Step 3: Print the result. Step 4: EndExample Code Live Demo#include #include using namespace std; main() {    int n;    char num_string[20] = "1234"; n = atoi(num_string); cout

Convert an integer to a hex string in C++

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

15K+ Views

In this program we will see how to convert an integer to hex string. To convert an integer into hexadecimal string we can follow mathematical steps. But in this case we have solved this problem using simple trick.In C / C++ there is a format specifier %X. It prints the value of some variable into hexadecimal form. We have used this format specifier to convert number into a string by using sprintf() function.Input: An integer number 255 Output: FFAlgorithmStep 1:Take a number from the user Step 2: Make a string after converting number using %X format specifier Step 3: Print ... Read More

Check if a string contains a sub-string in C++

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

7K+ Views

Here we will see how the string library functions can be used to match strings in C++. Here we are using the find() operation to get the occurrences of the substring into the main string. This find() method returns the first location where the string is found. Here we are using this find() function multiple times to get all of the matches.If the item is found, this function returns the position. But if it is not found, it will return string::npos.So for checking whether the substring is present into the main string, we have to check the return value of ... Read More

Case-insensitive string comparison in C++

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

7K+ Views

In C++ we have strings in standard library. In this program we will see how to check whether two strings are identical or not. In this case we will ignore the case.Here the logic is simple. We will convert the whole string into lowercase or uppercase strings, then compare them, and return the result.We have used the algorithm library to get the transform function to convert the string into lowercase string.Input: Two strings “Hello WORLD” and “heLLO worLD” Output: Strings are sameAlgorithmStep 1: Take two strings str1, and str2 Step 2: Convert str1, and str2 into lowercase form Step 3: ... Read More

C++ Program to Perform String Matching Using String Library

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

2K+ Views

Here we will see how the string library functions can be used to match strings in C++. Here we are using the find() operation to get the occurrences of the substring into the main string. This find() method returns the first location where the string is found. Here we are using this find() function multiple times to get all of the matches.If the item is found, this function returns the position. But if it is not found, it will return string::npos.Input: The main string “aabbabababbbaabb” and substring “abb” Output: The locations where the substrings are found. [1, 8, 13]AlgorithmString_Find(main_str, sub_str)Input ... Read More

Advertisements