C++ strings allocate memory dynamically. More memory can be allocated to the string during run time if needed. Since there is no memory pre-allocation, no wastage of memory. We can perform various operations on strings, including comparisons, concatenation, conversion, etc. In this C++ tutorial, you will learn:

What is a String? Declaring Strings C-Style Character String std::string Accessing string Values String Functions in C++ strcpy() strcat() strlen() strcmp()

Declaring Strings

C++ supports two types of string declarations:

C-style character string String class type

C-Style Character String

This type of string declaration was introduced in C programming language. C++ continues to support it. It’s simply a one-dimensional array of characters terminated with a null character (\0). A null-terminated string has characters that make up the string then followed by a null. Consider the string declaration given below: The above declaration creates a string that forms the word John. The word has 4 characters, but the string has a size of 5. The extra space allows for holding of the null character. Using the array initialization rule, we can write the above statement as follows: Note that you don’t have to place the null character at the end of the string constant. The C++ compiler will automatically place the ‘\0’ at the string’s end when initializing the array.

std::string

The standard C++ library provides the string class which supports various string operations. It is written as std::string. To use this class, we must first include it into our workspace using the #include preprocessor as shown below: Next, we can declare our string using the string keyword. For example: The above statement will create a string named name to hold the value John.

Accessing string Values

In C++, we can access the string values using the string name. For example: Output:

Here is a screenshot of the code:

Code Explanation:

Including the iostream header file in our code. It will allow us to read from and write to the console. Including the std namespace so as to use its classes and functions without calling it. Calling the main() function inside which the logic of the program should be added. The { marks start of body of the main() function. Declaring a string of characters and giving it the name name. The string will store the value John. The extra space will store the null character. Printing some text on the console. Printing the value of the string named name on the console. The main() function should return an value if the program runs fine. End of the body of the main() function.

Here is another example using the C++ standard string class: Output:

Here is a screenshot of the code:

Code Explanation:

Including the iostream header file in our code. It will allow us to read from and write to the console. Including the standard string class in our code. Including the std namespace so as to use its classes and functions without calling it. Calling the main() function inside which the logic of the program should be added. The { marks start of body of the main() function. Declaring a string and giving it the name name. The string will store the value Guru99. Printing the value of the string name alongside some text on the console. The main() function should return an value if the program runs fine. End of the body of the main() function.

String Functions in C++

You will often want to manipulate strings. C++ provides a wide range of functions that you can use for this. These functions are defined in the CString class, hence, we have to include it in our code in order to use the functions. Let us discuss some:

strcpy()

This is the string copy function. It copies one string into another string. Syntax: The two parameters to the function, string1 and string2, are strings. The function will copy the string string1 into the string 1.

strcat()

This is the string concatenate function. It concatenates strings. Syntax: The two parameters to the function, string1 and string2 are the strings to be concatenated. The above function will concatenate the string string2 to the end of the string string1.

strlen()

This is the string length function. It returns the length of the string passed to it as the argument. Syntax: The parameter string1 is the name of the string whose length is to be determined. The above function will return the length of the string string1.

strcmp()

This is the string compare function. It is used for string comparison. Syntax: The above function will return 0 if strings string1 and string2 are similar, less than 0 if string1<string2 and greater than 0 if string1>string2.

Example:

The following example demonstrates how to use the above string functions: Output:

Here is a screenshot of the code:

Code Explanation:

Including the iostream header file in our code. It will allow us to read from and write to the console. Including the standard CString class in our code. Including the std namespace so as to use its classes and functions without calling it. Calling the main() function inside which the logic of the program should be added. The { marks start of body of the main() function. Declaring a string of 10 characters and giving it the name name1. The string will store the value Guru99. Declaring a string of 10 characters and giving it the name name2. The string will store the value John. Declaring a string of 10 characters and giving it the name name3. Declaring an integer variable named len. Copying the string name1 into the string name3. Printing the value of the string name1 alongside some text on the console. It should print Guru99. Concatenating the strings name2 to the end of string name1. The value of name1 is now Guru99John. Printing the value of the string name1 alongside some text on the console. It should print Guru99John Determining the length of the string named name1 and assigning the value of length to variable len. Printing the value of len variable alongside some other text on the console. The main() function should return an value if the program runs fine. End of the body of the main() function.

Summary

A string is a sequence of characters. Strings belong to the standard string class in C++. We can declare strings using the C-style character string or standard string class. The strcpy() function copies one string into another. The strcat() function concatenates two functions. The strlen() function returns the length of a function. The strcmp() function compares two strings.