C++ Articles - Page 652 of 719

string at() function in C++

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

153 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++?

Farhan Muhamed
Updated on 09-Jun-2025 19:09:19

88K+ 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 String = "Hello TutorialsPoint" Char = 'o' // Output String = "Hell TutrialsPint" Algorithm to Remove Certain Characters from a String Here is the algorithm to remove certain characters from a string in C++: Step 1: Take a string and a character to be removed ... Read More

How to quickly reverse a string in C++?

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

367 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?

Farhan Muhamed
Updated on 06-Jun-2025 18:58:17

3K+ Views

Here we will see how to map 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. First of all, let us understand what is an enum in C++. What is an Enum in C++? Enum is a user-defined datatype in C++ that is used to assign names to integer values. For example, you ... Read More

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

Farhan Muhamed
Updated on 04-Jun-2025 18:48:16

9K+ Views

The STL library in C++ provides various inbuilt functions and methods to manipulate strings, such as converting them to lower case or upper case. In this article, we will learn all the approaches to convert a std::string to lower case in C++. First of all, let's understand the problem statement. In this problem, you are given a string as input and you need to convert it to lower case. The string may contain any type of characters such as letters, digits, and special characters. For example: // Input String std::string str = "Hello World! 123 @ TutorialsPoint"; // ... Read More

How to convert an enum type variable to a string in C++?

Farhan Muhamed
Updated on 06-Jun-2025 18:57:32

580 Views

Enum is a user-defined datatype in C++ that is used to assign names to integer values. For example, you can use an enum to represent the days of the week from 0 to 6, where 0 represents Sunday, 1 represents Monday, and so on. When you try to access a value of an enum, it will only return an integer value. In this article, we will learn how to convert an enum type variable to a string type variable in C++, so that we can easily display the enum value as a string. Example of Enum: enum color ... Read More

How to convert a std::string to const char* or char* in C++?

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

1K+ Views

In this section, we will see how to convert C++ string (std::string) to const char* or char*. These formats are C style strings. We have a function called c_str(). This will help us to do the task. It returns a pointer to an array that contains a null-terminated sequence of characters (i.e., a C-string) representing the current value of the string object.Following is the declaration for std::string::c_str.const char* c_str() const;This function returns a pointer to an array that contains a null-terminated sequence of characters (i.e., a C-string) representing the current value of the string object. If an exception is thrown, ... Read More

Differences between C++ string == and compare()?

Farhan Muhamed
Updated on 26-May-2025 18:17:37

4K+ Views

In C++ we can compare two strings using compare() function and the == operator. Then the question is why there are two different methods? Is there any difference or not? Yes, there are some basic differences between compare() and == operator. In C++ the == operator is overloaded for the string to check whether both strings are same or not. If they are the same this will return 1, otherwise 0. So it is like Boolean type function. The compare() function returns two different things. If both are equal, it will return 0, If the mismatch is ... Read More

Difference between string and char[] types in C++

Farhan Muhamed
Updated on 26-May-2025 18:18:53

850 Views

In C++, char[] represents a character array, while string is a class in STL to manage text data. Both string and char[] can be used to store and process text data. But, they are different in terms of structure, usage, memory management, and capabilities. In this article, we will discuss the difference between string and char[] types in C++. char[] in C++ char[] is an array of characters that is used to store a string. It is a C-style string representation. The size of char array will be fixed. At the end of the array, a null character '\0' ... Read More

C++ Program to Implement String Matching Using Vectors

Farhan Muhamed
Updated on 04-Jun-2025 16:25:31

647 Views

In C++, we can create vectors easily using the standard library. We are taking the main string and the string that will be searched as a vector, then searching it into the main string. When one match is found the function returns the address, and removes it from the main string. So in the next iteration, it starts from location 0 and searches again. For multiple occurrences, we are using loops and repeatedly searching for the match, and return the position. Input: Main String: "ABCcdABCcdABC" Substring to search: "cd" Output: Match found at Position = 1 Match found at ... Read More

Advertisements