What are strings in C++?
July 11, 2022
What are strings? Is it another data structure? Is it something else? Let’s find out 🙌
Strings are everywhere. This text that you see is a string. This whole blog post can be a string!
What are strings though?
In the simplest words, string is a sequence of characters.
Does “sequence of characters” remind of you something else? Yes you’re thinking in the right direction. Arrays are also a sequence of similar type of data.
What is the difference between strings and array then?
Array is a data structure whereas string is either an object or an array of characters 🤯
You must be wondering what is this right?
It is confusing. But let’s break it down. In C++, 2 types of strings are commonly used:
1. Strings that are objects of string class (remember objects and classes from OOPS?)
2. Strings that are arrays with data type char and terminated with \0 - also called as C-style strings.
Even C++ string class internally uses char array to store characters but all memory management, allocation, and null termination is handled by string class itself. That is why it is easier to use than the char array.
This is why string can either be an object or an array of characters depending on how it is being used.
We will talk about the first type of strings here, i.e. strings that are objects of string class. In this case string becomes a data type (a user-defined data type).
Properties of strings
A string is an array of characters, but it behaves differently in many ways.
1. Size of a string
When declaring an array, you specify the size in advance, but in case of a string, the size could be different depending on input.
If you want to find out how many characters are there in a string, you can use the inbuilt function length. Here's how you can use it 👇
The function returns an integer representing the number of bytes in the string. The output of the above code would be
2. Accessing individual elements in a string
Strings and arrays are similar in this case. You can access all elements of a string individually. Strings also have the concept of indices, just like arrays. You can use these indices to access a particular position, perform operations on it, and even change the string just like arrays 😌
How do I input a string?
Since string here is a data type, you can declare it using string stringName and take input using the cin operator.
But here's a catch - cin treats every whitespace as a terminating character.
What is a terminating character?
It is anything that marks the end of input. If we say that cin treats every whitespace character as terminating character, it implies that it will stop taking any input after a space, or tab, or enter.
If you want to include whitespaces in your string, you can use the getline function for input. It takes cin as the first parameter, and the string variable as second. Here’s the syntax for the same 👇
How do I output a string?
Do you remember how an array is printed? If you remember it then kudos and if not then refer to this post.
You cannot output an array without a loop.
However, when it comes to string, you can directly print it this way 👇
This will give you "Hello". You do not need any loop if you want to print the string as it is.
Hope this is helpful and gives you a basic idea on what strings exactly are in C++ 😌 You can explore more about the standard string class here. You can send me a mail at email@example.com or DM me on Instagram if you have any questions 💪
Until next time 👋
Want to learn more?
Our first course, Introduction to Data Structures in C++ worth ₹499/month is available at a price of your choice for the first month access.
Offer available for a limited time