Strings in C++

Strings in C++ with Examples:

In this article, I am going to give you a brief introduction to Strings in C++ Language with examples. Please read our previous section, where we discussed Pointers in C++ with examples. Strings is one of the powerful features of C++.

What is a String?

String is a collection of characters or letters. By grouping the letters, we can form a word or a name like Rohan. Not only the person’s name, we can also form the name of place, city or can be the complete address like ‘California’, ‘Mumbai’ or can also be a sentence like ‘Rohan went to park’.

A string can also be a paragraph. We can have different size of string. The largest size is allowed up more than 2000 characters. So, it depends on the compiler and its versions.

We can use the strings for storing names or words or sentences or paragraphs. As we said string is a collection of characters. So let us see that.

In the string ‘Rohan’, ‘R’, ‘o’, ‘h’, ‘a’ and ‘n’ are the letters. It is a collection of alphabets. So, the set of alphabets together is forming a string. Now we will see how to represent the string in C++ as well as we will also learn the possible operations that can be performed on the strings in C++ Language.

How to represent string in C++?

There are two methods of representing a string in C++. They are as follows

  1. Using array of characters
  2. Class String

One is using array of characters, as we said that string is a set of characters, that is array of characters. This method is available in C language and also it can be used in C++.

And the second method is, C++ also providing a class called ‘String’. So, in C++ we prefer using this class whenever we want to store a string. We will learn about both of these methods in detail. So let us start with string using character.

String Representation using Array of Character in C++:

Here, we will discuss how we can declare a string and how we can initialize it. For better understanding, please have a look at the below image.

String Representation using Array of Character in C++

Here in the above example, c is a character type variable which can store only a single letter. Character takes one bite of memory, so ‘z’ is stored in that one. Actually, the ASCII code of ‘z’ will be stored in that one bite of memory. Character type variable can store just a single letter. They cannot store a string. Let us see how to store a string. For better understanding, please have a look at the below image.

Strings in C++ with Examples

To store a string, here, we have to take an array. So, this is an array of type character and when we use double quotes then it’s a string i.e. “Rahul”. If you observe, we have used single quotes for character constant and here for a string constant, we used double quotes. We will call it as a string literal.

What happens when you declare an array and also initialize it with the string?

See here array of size ‘10’ will be created. As we have mentioned size as ‘10’. And in this array, all these alphabets are stored. So, each location can store a single character.

One of the important things that we have to learn is that, at the last of the array there is a null character ‘\0’ or string delimiter. This is a combination of two characters that is backslash and zero. Its value is zero. It is also called string terminator.

In C as well as in C++ when you are using an array of characters then definitely the string will be having ‘\0’ at the end. You can observe that we have taken an array of size ‘10’, so the capacity of this array is ‘10’ but we have stored only 5 letters in this array. Then there must be some character to show where this string is ending. So, ‘\0’ acts as a string terminator.

Declaration and Initialization of String in C++:

Now, let us understand the Declaration and Initialization of String in C++. For better understanding, please have a look at the below image.

Declaration and Initialization of String in C++

Here we have declared a character array and also initialize this character array with the string “Rohan”. Here, we haven’t mentioned the size, in our previous method the size was mentioned but here it’s not mentioned. In this case, it will automatically create an area of exact size including the space for a string terminator that is null character.

Now let us look at next method.

What happens when you declare an array and also initialize it with the string?

Here we have not mentioned the size but we are initializing a character array. This is just like initializing character array but this null character is also included and inclusion of this character makes it as a string. Without this character it is not a string.

Then one more method here.

What is a String?

We can also use the ASCII codes of the alphabet. So, we have use 82, 111, 104, 97, 110. So actually alphabets ‘R’, ’o’, ’h’, ’a’, ’n’ will be stored. Then at the end, we added null character.

There is one more method that is using a pointer we can also create a string in C++.

Strings in C++ Language with examples

Here ‘S’ is a pointer that is pointing on the string ‘Rohan’. So broadly there are two methods. One is using a character array that we saw in previous method and second one is using a pointer.

Mostly we use a pointer to a string if we want to have a string inside heap. So, we just declare it and here we have initialized it. So “Rohan” is nothing but a string literal. Literals are created inside code sections.

So, if you want a string in heap then go for character pointer. If you a string in stack then go for character. Now let us see all these method in a C/C++ program.

String Declaration, Initialization and Representation Program in C++:
#include <stdio.h>
#include <stdlib.h>

int main()
    char c = 'z';		// to store single character
    char S[10] = "Rohan";	// with size
    char A[] = "Rohan";		// without size
    char B[] = { 'R', 'o', 'h', 'a', 'n', '\0' };
    char C[] = { 82, 111, 104, 97, 110, '\0' };	// with ASCII codes
    char *D = "Rohan";

    printf("%c\n", c);
    printf("%s\n", S);
    printf("%s\n", A);
    printf("%s\n", B);
    printf("%s\n", C);
    printf("%s\n", D);

String Declaration, Initialization and Representation Program in C++

In the next article, I am going to discuss Reading and Writing Strings in C++ with examples. Here, in this article, I try to explain Strings in C++ Language with examples. I hope you enjoy this Strings in C++ with examples article. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

Your email address will not be published. Required fields are marked *