basic_string c_str function in C++ STL Last Updated : 07 Feb, 2023 Summarize Suggest changes Share Like Article Like Report The basic_string::c_str() is a built-in function in C++ which returns a pointer to an array that contains a null-terminated sequence of characters representing the current value of the basic_string object. This array includes the same sequence of characters that make up the value of the basic_string object plus an additional terminating null-character at the end. Syntax: const CharT* c_str() const Parameter: The function does not accept any parameter. Return Value : The function returns a constant Null terminated pointer to the character array storage of the string. Below is the implementation of the above function: Program 1: CPP // C++ code for illustration of // basic_string::c_str function #include <bits/stdc++.h> #include <string> using namespace std; int main() { // declare a example string string s1 = "GeeksForGeeks"; // check if the size of the string is same as the // size of character pointer given by c_str if (s1.size() == strlen(s1.c_str())) { cout << "s1.size is equal to strlen(s1.c_str()) " << endl; } else { cout << "s1.size is not equal to strlen(s1.c_str())" << endl; } // print the string printf("%s \n", s1.c_str()); } Outputs1.size is equal to strlen(s1.c_str()) GeeksForGeeks Program 2: CPP // C++ code for illustration of // basic_string::c_str function #include <bits/stdc++.h> #include <string> using namespace std; int main() { // declare a example string string s1 = "Aditya"; // print the characters of the string for (int i = 0; i < s1.length(); i++) { cout << "The " << i + 1 << "th character of string " << s1 << " is " << s1.c_str()[i] << endl; } } OutputThe 1th character of string Aditya is A The 2th character of string Aditya is d The 3th character of string Aditya is i The 4th character of string Aditya is t The 5th character of string Aditya is y The 6th character of string Aditya is a Program 3: C++ // CPP program to copy the string // using std::string::c_str() method #include <bits/stdc++.h> // Function to copy the string const char* copyString(std::string s) { const char* s2; // std::string::c_str() method s2 = s.c_str(); return s2; } // Driver Code int main() { std::string s1 = "GeeksforGeeks"; std::string s2; // Function Call s2 = copyString(s1); std::cout << s2; return 0; } // This code is contributed by Susobhan Akhuli OutputGeeksforGeeks Advertise with us Next Article str_c() Function of stringr Package in R A AdityaTiwari5 Follow Similar Reads strtol() function in C++ STL The strtol() function is a builtin function in C++ STL which converts the contents of a string as an integral number of the specified base and return its value as a long int. Syntax: strtol(s, &end, b) Parameters: The function accepts three mandatory parameters which are described as below: s: s 4 min read strcspn() function in C/C++ The strcspn() function in C/C++ takes two string as input, string_1 and string_2 as it's argument and searches string_1 by traversing for any characters that is present in string_2 . The function will return the length of string_1 if none of the characters of string_2 are found in string_1 . This fu 2 min read String Functions in C++ A string is referred to as an array of characters. In C++, a stream/sequence of characters is stored in a char array. C++ includes the std::string class that is used to represent strings. It is one of the most fundamental datatypes in C++ and it comes with a huge set of inbuilt functions. In this ar 8 min read str_c() Function of stringr Package in R In this article, we will discuss str_c() function which is available in stringr package. Before going to this method, we have to install and import stringr package. Syntax: Install- install.packages("stringr") Import - library("stringr")str_c() Methods This function is used to combine the multiple s 1 min read C String Functions C language provides various built-in functions that can be used for various operations and manipulations on strings. These string functions make it easier to perform tasks such as string copy, concatenation, comparison, length, etc. The <string.h> header file contains these string functions.Th 6 min read std::basic_string::at in C++ Returns a reference to the character at the specified location pos. The function automatically checks whether pos is the valid position of a character in the string (i.e., whether pos is less than the string length), throwing an out_of_range exception if it is not. Syntax: reference at (size_type po 1 min read Article Tags : C++ Practice Tags : CPP Like