C++ Strings5 min read

A string is a sequence of characters that is an object of std::string class in C++. Memory to a string is allocated dynamically that is we can assign memory during runtime as required. Various operations are performed on a string such as concatenation, comparison, conversion, etc.

C++ supports two types of string in a program.

  • C-style character string.
  • String class type (Standard C++ Library).

C-style character string

C-style was introduced in the C programming language and is continued to be supported by C++. These one-dimensional strings are stored in a series in an array of type char and are terminated by a null character ‘\0’.

Memory presentation of the character array in C/C++:

string

Declaring C-style string:

char name[5] = {'A', 'l', 'e', 'x', '\0'};

The above character for a string ‘Alex‘. But notice that the number f character is 4 but we declare the size 5 as because the last space is reserved for the null character in order to terminate the string.

Although, we can also initialize the above in the following way.

char name[] = "John";

Note that here, we do not have to add a null character at the end. The C++ compiler will automatically place the ‘\0‘ at the end of the string.

Example: C++ program to read and display the string

Enter a string: This is simple2code.com
Entered String is: This is simple2code.com

Notice that we use cin.get in order to get the string from the user that contains white spaces in a string. It takes two argument name of a string and the size of an array.


String class type

The standard C++ library provides a string class that supports the various operation on a string. It is written as std::string.

Also, instead of char type, we create a string object to hold the string. It does not require fined length like in char type, we can extend the character according to our need.

Now in order to use the class to perform various operations on a string, we need to include the following header file.

#include<cstring>

Example: C++ program to read and display the string using string object

Before performing on class type with some operation, let us first see a simple example of string using its object.

Enter a string: This is simple2code.com
Entered String is: This is simple2code.com

Notice that to get the string from the user, we use getline() function that takes two arguments – input stream (cin) and location of the line to be stored (str).


C++ String Functions

There are different functions provided by the string class to manipulate strings in a program. The cstring class defines these functions and we need to include cstring header file in a program. Let us see some of these functions with examples.

FunctionDescription
strcpy(str1, str2);It copies string2 content in string1.
strcmp(str1, str2);It is a comparison function.
returns 0, if str1 = str2
returns less than 0, if str1<str2
returns greater than 0, if str1>str2
strcat(str1, str2);It joins str2 into the end of str1.
strlen(str);It returns the length of the string passed.

Example of the above string function in C++

Output:

strcpy( str3, str2) : John
strcat( str1, str2): MarkJohn
strlen(str1) : 8
strcmp(str2, str3) : 0


Passing String to a Function

We pass the string in a similar way like we pass an array to a function. We will see an example where we will learn to pass two different strings in a function.

It is a basic example, where we create two print functions and take the user input for two different strings.

Enter First string: This is a website
Enter Second string: It is simple2code.com

String is: This is a website
Char array is: It is simple2code.com


MORE

Keith Number Program in C

In this tutorial, we will write a Keith number in C. It is one of the most asked questions in an interview. Before that, you …
Read More

Keith Number Program in C++

In this tutorial, we will write a Keith number in C++. It is one of the most asked questions in an interview. Before that, you …
Read More

Singly linked list Program C++

In this tutorial, we will write a C++ program to implement singly linked list. In this program, we will create a separate function for each …
Read More

Fascinating Number Program in C

In this tutorial, we will write a C program to check whether the given number is a Fascinating number or not. You may go through …
Read More

Peterson Number in C

In this tutorial, we will learn about the Peterson number and check if the number is Peterson or not in C. What is Peterson Number? …
Read More

Peterson Number in C++

In this tutorial, we will learn about the Peterson number and write a C++ program to check if the given number is a Peterson number …
Read More