C String: All You Need to Know About It - JohnHau/mis GitHub Wiki
Table of Contents What is a C++ String?C-Style Character StringC++ String Class Introduced in C++What is the Difference Between C++ String and Character Array?C++ String Built-In Functions to Perform Operations on StringsView More C++ is a widely popular general-purpose programming language based partially on Object-Oriented Programming (OOPs) concepts. It allows you to work with text and characters through C++ strings. In this article, you will learn everything about the String class.
What is a C++ String? C++ string is a way of representing and working with texts and characters. It has two different types of string representations: C-style character string and String class introduced in C++.
C-Style Character String The C-style character string was a part of the C programming language and is now supported in C++. It is like a one-dimensional character array that holds a character sequence and is terminated with a null character. Thus, if you create a C-style character string to keep the word “Hello,” it will contain the characters, ‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘\0.’
Here, you only need to ask the compiler to hold the word “Hello” in a string. But it will still include a character “\0” in the end by default. Look at an example to see how you can use the character array to create and store a C-style character string in a variable.
#include
using namespace std;
int main (){
char greet[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
cout << "This is a greeting message: ";
cout << greet << endl;
return 0;
}
C++ String Class Introduced in C++ C++ is based on the OOPs concept; it enables you to represent the string as an object of the C++ String class (std:: string). The class allows you to declare a string variable quickly, and store any sequence of characters in it. Here’s an example of representing a string with the help of the String class.
#include
using namespace std;
int main(){
string greet = "Hello";
cout << "This is a greeting message: ";
cout<<greet<<endl;
}
What is the Difference Between C++ String and Character Array? Although both the C-style character string and the C++ String class are used for string representation, there are a few differences. Some of the primary differences are:
Character array is an array, but a string is a class that defines an object. Character array size is pre-allocated statically. Hence, extra memory is not available during runtime, and it wastes the memory that is not used. Since string size is not pre-allocated, there is no wastage, and extra memory is available during run time. Character array has the risk of array decay, but that is not the case with string. Strings are slower than character arrays when it comes to implementation. The String class offers more in-built functions to work with and manipulate strings. C++ String Built-In Functions to Perform Operations on Strings As mentioned in the previous section, the C++ String class provides many built-in, predefined functions to manipulate strings. In this section, you will go through some primary functions and see them in action, along with examples.
C++ String Input Functions
Example: Using C++ String Input Functions #include
#include
using namespace std;
int main(){
// Declaring a string variable
string s;
cout << "Enter a string: ";
// Using getline() to accept input
// We will give Simplilearn as the input
getline(cin,s);
// Displaying the entered string
cout << "This is initial string : ";
cout << s << endl;
// Inserting a character i at the end of the string
// using the push_back function
s.push_back('i');
// Displaying the string after push_back
cout << "The new string is : ";
cout << s << endl;
// Deleting the i from the end using pop_back
s.pop_back();
// Displaying the string after pop_back
cout << "After pop_back operation, the string is : ";
cout << s << endl;
return 0;
}
Example: Using String Class’s Iterator Functions #include
#include
using namespace std;
int main(){
string s = "Simplilearn";
// Declaring the iterator
std::string::iterator iter;
// Declaring iterator for reverse functions
std::string::reverse_iterator iter1;
// Displaying string
cout << "Using forward iterators : ";
for (iter=s.begin(); iter!=s.end(); iter++)
cout << *iter;
cout << endl;
// Displaying reverse string
cout << "Using reverse iterators : ";
for (iter1=s.rbegin(); iter1!=s.rend(); iter1++)
cout << *iter1;
cout << endl;
return 0;
}
Here, you used the for loop in the code and both forward and reverse iterators to traverse through the string. The first for loop used a forward iterator and kept traversing and printing characters until it reached the end of the string. In the second for loop, you used the reverse iterator that started from the end and kept traversing and printing the characters until they reached the string’s start.
Example: Using String Class’s Capacity Functions #include
#include
using namespace std;
int main(){
string s = "C++ Programming is Awesome!";
cout << "Initial string : ";
cout << s << endl;
// Resizing string using resize()
s.resize(15);
cout << "String after resizing : ";
cout << s << endl;
// Using the capacity function
cout << "String's capacity is : ";
cout << s.capacity() << endl;
// Using the length function
cout<<"String's length is : "<<s.length()<<endl;
// Using the shrink_to_fit function
s.shrink_to_fit();
cout << "Capacity post shrinking is : ";
cout << s.capacity() << endl;
return 0;
}
Example: Using String Class’s Manipulating Functions #include
#include
using namespace std;
int main(){
string s1 = "Welcome, learn C++ with Simplilearn here";
string s2 = "C++ is Awesome!";
// Declaring character array
char ch_arr[80];
// Using copy() to copy elements from s1 and
// placing it in ch_arr
s1.copy(ch_arr,26,9);
// Printing the array
cout << "Character array : ";
cout << ch_arr << endl << endl;
// Strings before swapping
cout << "String1 before swapping is : ";
cout << s1 << endl;
cout << "String2 before swapping is : ";
cout << s2 << endl;
// using swap() function
s1.swap(s2);
// Strings after swapping
cout << "String1 after swapping is : ";
cout << s1 << endl;
cout << "String2 after swapping is : ";
cout << s2 << endl;
return 0;
}
Conclusion In this article, you learned everything about the C++ String class, character array, and the difference between the two. You have also gone through some of the primary in-built String class functions that allow you to perform various operations on strings, along with examples. You can now use multiple functions to work with texts and strings in C++. Learning to work with C++ strings is essential, as you might have to take various inputs and give outputs through your app.
If you want to learn more about such fundamental C++ concepts, refer to Simplilearn’s C++ Tutorial for Beginners. The tutorial covers basic but most essential concepts like a classes, array and for loop in C++. Thus, it is adept at helping you set the foundation to excel in C++ development.
Are you looking to be an expert programmer? If yes, Simplilearn’s Post Graduate Program in Full Stack Web Development in collaboration with Caltech CTME should be your next stop.
Have any questions for us? Leave them in the comments section of this article. Our experts will get back to you on the same, ASAP!