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

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …
Read More

Java Program to Find pair of Integers in Array whose sum is given Number

In this tutorial, we will write a program to find a pair of elements from an array whose sum equals a given number in java …
Read More

Program to Print Diamond Alphabet Patterns in C

In this tutorial, we will learn to write a C program to print Diamond patterns using alphabets/characters. However, in this tutorial, we will create a …
Read More

Half Diamond Pattern in C using Alphabets

In this tutorial, we will learn and code the half diamond alphabet patterns in C programming language. However, in this tutorial, we will create a …
Read More

Half Pyramid of Alphabets in C

In this tutorial, we will learn and code alphabet patterns in C programming language specifically the Half pyramid of alphabets in C programming. However, in …
Read More

Inverted Half Pyramid Pattern of Alphabets in C

In this tutorial, we will write a C program to print half Pyramid using alphabets/characters. Before that, you may go through the following topic in …
Read More