Tag: Char
Convert char to int in C++
In this article, we will discuss how to convert a character to an integer in C or C++. Table of Contents Convert a char to an ascii int value Convert char to actual int using Typecasting Convert char to actual int using stringstream Convert a char to an ascii int value If we directly cast …
How to Convert a char array to String in C++?
In this article, we will discuss different ways to convert a char array to a string in C++. Table Of Contents Problem Description Convert char array to string using simple addition Convert char array to string using stringstream Convert char array to string using push_back() function Summary Problem Description We have given an array that …
How to convert a string to char array in C++?
In this article, we will discuss different ways to convert a string to a char array in C++. Table Of Contents Problem Description Convert string to char array using strcpy() function Convert string to char array using string::copy() function Convert string to char array using STL’s copy() function Summary Problem Description We have given a …
Convert string to const char* in C++
In this article, we will discuss different ways to convert a string to const char* in C++. Table Of Contents Method 1: Using string::c_str() function Method 2: Using string::data() function Method 3: Using [] operator Summary Method 1: Using string::c_str() function In C++, the string class provides a member function c_str(). It returns a const …
Convert a char to string in C++
In this article, we will discuss different ways to convert a char value to a string in C++. Table Of Contents Method 1: Using string constructor Method 2: Using string::push_back() function Method 3: Using std::stringstream Method 4: Using += operator Method 5: Using assignment operator Method 6: Using string::append() function Method 7: Using string::assign() function …
Convert string to char array in C++
In this article, we will discuss different ways to convert a string to a char array in C++. Table Of Contents Method 1: Using string::copy() Method 2: using strcpy() Summary Method 1: Using string::copy() Create an array of same length as string, and initialize it with null characters. Then use the copy() function of string …
Convert string to char* in C++
In this article, we will discuss different ways to convert a string to char* in C++. Table Of Contents Method 1: Using string::c_str() function Method 2: Using string::data() function Method 3: Using [] operator Method 4: Using STL Algorithm copy() Summary Method 1: Using string::c_str() function In C++, the string class provides a member function …
Compare a String and a char array in C++
This tutorial will discuss about unique ways to compare a string and a char array in C++. Table Of Contents Technique 1: Using string::compare() function Technique 2: Using strcmp() function Summary Technique 1: Using string::compare() function The string class in C++, provides a function compare(). It accepts another string or a character pointer, as an …
Get Char from String by Index in C++
This tutorial will discuss about unique ways to get a character from string by index in C++. Table Of Contents Technique 1: Get character at specific index Technique 2: Using [] Operator Summary Technique 1: Get character at specific index The string class provides a member function at(). It accepts an index position as an …
Check if Char Array contains a string in C++
This tutorial will discuss about unique ways to check if char array contains a string in C++. Table Of Contents Technique 1: Check if a char array contains a substring Technique 2: Check if a char array contains a substring Summary Technique 1: Check if a char array contains a substring The strstr() function takes …