DEV Community

Cover image for C++ Strings
Sushant Gaurav
Sushant Gaurav

Posted on • Updated on

C++ Strings

Abstract

Strings are variables or constants or literals that contain a sequence of characters. Strings are very frequently used by programmers because of their flexibility and predefined functions support. In C++, we have two types of strings: the first one is C-Language style strings and the second one is String Class (a part of the Standard Template Libraries). We can perform various operations on strings such as concatenation, comparison, copy, conversion, etc.

Scope of Article

The article contains topics such as Strings, how they are used in C++, the various functions associated with them. The article also compares the normal Strings (used in different languages) to the String class that comes with the C++ STL.
Each of the topics is explained clearly with code and examples wherever necessary.

Contents

  • Definition.
  • Types of Strings in C++.
  • Some useful functions of the String class.
  • Strings v/s character array.
  • Why use Strings?
  • Summary.

Definition

Strings are nothing but variables that are used to store a sequence or collection of characters. Sometimes they are compared with character arrays (character arrays are also sequences of characters) but the main difference between them is that strings can use standard operators of C++ whereas character arrays cannot. This is one of the main reasons why strings are most widely used by programmers.

Types of Strings in C++

In C++, we have two types of strings:

1. The C-Language or primitive type string:

This string is originated in the C Language which is also known as the predecessor of C++. These strings are nothing but a character array or one-dimensional array of characters. As they are character arrays, so they have a fixed length that must be determined at the time of creation only. These strings end with a special character called the “null” character. The null character at the end denotes the termination of a string; it is represented using ‘\0’ and has an ASCII value 0x00.

NOTE: The null character should also be considered at the time of counting the length of the string. Let’s take an example and store “hello” to understand strings better.

char example[] = {‘h’, ‘e’, ‘l’, ‘l’, ‘o’, ‘\0’}; 
Enter fullscreen mode Exit fullscreen mode

or

char example[] = “hello”;
Enter fullscreen mode Exit fullscreen mode

This initialization is the same as an integer array with one notable difference i.e., the last character is a null character, So, the size of the array is 6, but the length of the string is 5 since the null byte never counts towards the length of the string.

2. The STL version or String Class:

String Class is one of the classes of the C++ standard library. They are strings that support different operations and have a lot of handy predefined functions. One of the major advantages of string class is that string objects have no fixed length; hence we can extend their length as per our need. To use strings, we must include header file and create an object of string class.

NOTE: We can also use , as it is a collection of all the standard template libraries. Let’s take an example to understand it better.

std::string example = “hello world”;
Enter fullscreen mode Exit fullscreen mode

Some useful functions of the String Class

1. length()

It returns the length, or the number of characters present in the string.
Example:

string str = “hello”; 
cout << str.length(); 
Enter fullscreen mode Exit fullscreen mode

Output: 5

2. push_back()

This function is used to insert a character at the end of the string.
Example:

string str = “hello”; 
str.push_back(“!”); 
cout << str; 
Enter fullscreen mode Exit fullscreen mode

Output: hello!

3. pop_back()

This function is used to delete the character from the end of the string.
Example:

string str = “hello!”; 
str.pop_back(); 
cout << str; 
Enter fullscreen mode Exit fullscreen mode

Output: hello

4. append()

This function is used to concatenate two strings.
Example:

string str1 = “hello ”; 
string str2 = “world”; 
cout << str1.append(str2); 
Enter fullscreen mode Exit fullscreen mode

Output: hello world

Note: We can also use the “+” operator to append these strings. But the append operation works faster than the + operator.
Example:

string str1 = “hello ”; 
string str2 = “world”; 
cout << str1 + str2; 
Enter fullscreen mode Exit fullscreen mode

Output: hello world

5. compare()

This function returns an integer and is used to compare two strings. Depending on the integer returned, we can know the relation between the strings.

  • If the returned value is 0, it means both the strings are equal.
  • Else, the strings are not equal.

Example:

string str1 = “hello”; 
string str2 = “hello”; 
if (str1.compare(str2) == 0) 
      cout << “Strings are equal.”; 
else 
      cout << “Strings are not equal.”;
Enter fullscreen mode Exit fullscreen mode

Output: Strings are equal.

Some other commonly used functions are
  • begin()
  • capacity()
  • clear()
  • copy()
  • empty()
  • erase()
  • find()
  • replace()
  • resize()
  • size()
  • swap()

Strings v/s character array

A string is a sequence of characters treated as a single data type on the other hand character array is a collection of char data types. The string is a class, but the character array is a data structure. String does not have a fixed length, but arrays have a fixed length that must be determined at the time of the creation. We can use standard C++ operators on the string, but we cannot use them on a regular character array. The String boundaries do not overrun but the character array boundaries are easily overrun.
On the other hand, one of the disadvantages of the string is that it is slower than the character array in terms of accessing its value.

Why use Strings?

The string object is treated as a variable and is changeable in size. This makes the string more useful than the character array. Apart from the size concept, there are a lot more advantages associated with string which makes it useful. Some of them are mentioned in bullet form below:

  • Null termination and memory allocation are done by the class itself.
  • Strings supports the standard C++ operators due to operator overloading.
  • Strings has a wide variety of member functions associated with it (refer to the content for details).
  • Strings supports type conversions.

Summary

  • The string is a sequence or collection of characters.
  • Strings are of 2 types in C++, one is the C-language string or the character array and the second one is the string class of STL.
  • Strings have more advantages than the regular character array.
  • Strings support standard operators of C++ and a wide variety of member functions as well.
  • Strings terminate with a null character.
  • Strings are frequently used by programmers because programmers need a way to store written human text.

Top comments (2)

Collapse
 
pauljlucas profile image
Paul J. Lucas • Edited

Strings are variables ...

Strings can also be constants and literals.

... a sequence or collection of characters.

It's always a sequence. "Collection" doesn't imply order.

Strings are very frequently used by programmers because of their flexibility and predefined functions support.

Strings are used because humans invented writing and programmers need a way to store written text.

This initialization is the same as an integer array with one notable difference i.e., the last character is a null character, So, the length of the string is 6.

No, the size of the array is 6, but the length of the string is 5 since the null byte never counts towards the length of the string. Indeed, strlen("hello") is 5, not 6.

string example = “hello world”;

As is, your example won't compile. It has to be std::string — or you need to have used using namespace std prior to that line.

Strings are mutable objects.

Not necessarily; for example:

std::string const s = "hello";
Enter fullscreen mode Exit fullscreen mode

is not mutable.

Collapse
 
imsushant12 profile image
Sushant Gaurav

Thanks for sharing, updated the details.