C++

How to Use the std::string

The std::string notation is yet another notation that is used for declaring strings in C++ in Ubuntu 20.04. There is no specific reason behind using this particular notation because it works exactly like the simple string notation. It is just another way of dealing with the strings in C++. For using this particular notation in C++, you have to include the “” library at the beginning of your C++ code. After that, you can conveniently use the std::string notation for declaring and initializing the strings in C++. In this article, we will talk about the methods of using the std::string notation in C++ in Ubuntu 20.04.

Using the std::string Notation in C++ in Ubuntu 20.04

We have designed the following six examples to teach you the usage of the std::string notation in C++ in Ubuntu 20.04. In these examples, our goal is to teach you the different ways of declaring and initializing the strings in C++. Along with that, we also wanted to teach you the method of taking single-word and multi-word strings as input from the user, checking the length of the string, and accessing any specific character of a string.

Example # 1: Declaring and Initializing a String

In this example, we will explain to you the method of declaring and initializing a string using the std::string notation in C++. The C++ program written for this purpose is shown in the image below:

In this C++ program, we have just included the “” library as an additional library. After that, we have declared our string “Name” using the statement “std::string Name” within our “main()” function. In the next step, we have initialized our string with the statement “Name = “John”. Then, we have just printed the initialized string on the terminal.

For compiling our C++ program stdString.cpp, we have used the following command:

$ g++ stdString.cpp –o stdString

Then, to execute this object file, we have used the command shown below:

$ ./stdString

As a result of executing this C++ program, the value assigned to the string is displayed on the terminal as shown in the following image:

Example # 2: Another Method of Declaring and Initializing a String

In this example, we just wanted to share an alternate method of declaring and initializing a string using the std::string notation in C++. For learning that, you can take a look at the C++ program shown in the image below:

In this example, the only statement that differs from our first example is “std::string Name = “John” i.e. the string declaration and initialization takes place on the same step. The rest of the code is accurately similar to our first example.

When we executed this code, the output turned out to be the same as the one shown in our first example because the functionality of our C++ program in this example was just like our first example. This is depicted through the following image:

Example # 3: Taking a String as an Input from the User

This illustration is slightly dissimilar from the first two examples. In this example, we want to take a string as an input from the user instead of initializing it on our own. For that, you can have a look at the C++ code shown in the image below:

In this example, we have just declared a string and left it uninitialized. Then, we have used the “cout” statement to display a message on the terminal to notify the user for providing a string input. After that, we have used the “cin” statement to take the user name as an input. Finally, we wanted to display the input taken from the user as it is on the terminal. A point to be noted over here is that this code will only take one-word string input and will discard any word provided after a space as an input.

When we executed this program, we were prompted to enter a user name as we did in the following image:

After entering the user name, it was printed as it is on the terminal as shown in the image below:

Example # 4: Taking Multi-Word String as an Input from the User

In this example, we will teach you the method of taking a multi-word string as an input from the user instead of a single-word string. For that, we have implemented the following C++ program:

In this C++ program, we have declared a string “Name”. After that, we have taken the name as an input from the user using the statement “std::getline(std::cin, Name)”. This statement will allow us to take a multi-word input from the user. Then, we just wanted to print this input on the terminal.

After executing this C++ code, we were prompted to enter a user name. We entered a user name based on two words separated by a space as shown in the image below:

The user input taken will be displayed on the terminal as shown in the following image:

Example # 5: Checking the Length of a String

This example will teach you the method of checking the length of a string using the std::string notation in C++. For doing so, we have implemented the C++ code shown in the image below:

In this example, we have initialized a multi-word string. After that, we just printed the length of this string on the terminal by using the “Sentence.length()” statement.

When we executed this C++ code, the length of our initialized string turned out to be “19” as shown in the following image. It is so because the length of a string in C++ is defined as the total number of characters in that string including the spaces.

Example # 6: Checking the Character at a Particular Position of the String

You can also find out any character at a specific position of a string. For that, you need to go through the C++ code shown in the image below:

In this C++ code, we have used the same string as we did in our fifth example. After that, we wanted to access the character present at the 4th position of our string. For accessing and printing that character on the terminal, we have used the “Sentence[3]” statement since the characters in a string are stored as a character array including the spaces.

When we executed this C++ code, the character at the 4th position of our string turned out to be “n” as shown in the following image:

Conclusion

This article was designed to explain to you the usage of the std::string notation of C++ in Ubuntu 20.04. We first explained why this particular notation is used in C++ followed by six different examples to elaborate this concept. These examples were designed to explain the usage of the std::string notation of C++ from scratch. Once you understand these examples, you will be able to use this notation very effectively while writing your C++ codes.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.