Working with strings in low-level languages like C and C++ is often considered painful. In this blog, we will simplify working with C++ String and understand the concepts at a much deeper level. This blog is a part of a series of blogs that I am writing on various C++ concepts. If you are entirely new to C++ I would recommend the following blog.
With that out of the way let’s discuss C++ strings on a deeper level.
What are C++ Strings
A string represents a word or a sentence. A C++ string is an array of characters. But they are a bit different from usual arrays as they end with a null character(\0). We will learn about the significance of the null character a bit later.
Basics of Array
Well, let’s cover strings in a bit more detail, for that we need to know what arrays are. Lemme give you a very basic introduction to arrays. Arrays are a basic data structure they are a contiguous block of memory.
Let’s use an analogy to understand what arrays are. Think of the memory as a series of houses in a line each having address say A, B, C, D, E, and so on. Say there are 25 houses. An array of 5 elements will be 5 houses located adjacent to each other. Say from house A to house E or from house B to house F or from house P to T. The only condition is that the next house should be adjacent to the previous one.
Hopefully, you get the gist. Similarly, our computer memory has an address for every unit. An array of 5 on memory will be 5 adjacent elements of memory say from address X to address X+5.

Consider the given example the 1, 2, 29, etc are values in our memory each block of memory has an address 00xA, 00xB, 00xC, etc this is a contiguous block of memory i,e, they are located adjacent to each other this is called an array.
datatype name[size] {value1,value2,...,valueN}
We use indexes to refer to the value stored in an array say we have the above array and we want to know what the first character is we just type the syntax.
cout << array_name[0] << endl;
We use 0 to refer to 1st element of the array i.e. indexing begins from 0. Similarly to access the 5th element from this array we do “array_name[4]” in the case of this array it will return 56.
Strings as an array of characters
Strings are very similar to arrays except they end with a ‘\0’ character and they can only store “char” values. “char” is a basic datatype in C++ it represents one character like ‘A’ or ‘a’. They are always written within single inverted commas. Each character maps to a numerical value for example the numerical value of ‘ ‘ i.e. white space is 32. We can find the numerical value of any character by doing a type conversion to ‘int’. The syntax to do it is as follows:
#include <iostream>
using namespace std;
int main()
{
char x;
cout << "Enter a character " << endl;
cin >> x;
cout << "Numeric value of character " << (int) x << endl;
return 0;
}
A representation of string in memory is as follows.

Types of C++ strings:
Mainly there are two types of C++ strings:
- C style string
- String class
We will discuss both C Style string and String class in great detail in the next section.
C Style Strings
C style strings are arrays of characters terminated by a null character. Let’s look at the syntax to initialize such strings. The syntax to initialize C style strings is given below:
char string[] {"Hello"};
C style strings are painful to work with as they have a fixed size. A C-style string is called a null-terminated string. The “cstring” library includes many functions to work with strings and as well as to manipulate them. Here’s a resource to learn about various functions that this library provides as I can’t look at every function. <cstring> (string.h) – C++ Reference
Here are some examples showing how to work with C-style strings.
1. Creating a string and printing it out
#include <iostream>
using namespace std;
int main()
{
char string[] {"Hello"};
cout << string << endl;
return 0;
}
2. Creating a string and finding the length
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char string[] {"Hello World"};
cout << string << endl;
cout << "Its size is " << strlen(string) << endl;
return 0;
}
3. Taking a C style string as a user Input.
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char string[20];
cout << "Enter a string: " << endl;
cin.get(string, 20);
cout << string << endl;
return 0;
}
Note: You can try using the “cin” instead of the “cin.get()” function but you will not be able to enter white spaces because the “cin” object treats whitespaces as null characters.
C++ String class
The string class allows us to work with C++ strings in a very abstract way. Working with string class is much easier than C style strings as they have a variable size and the “string” library provides way better functions to interact and manipulate strings. You may ask why we even need to learn about C style strings then this is because we have a lot of legacy code, they provide us a better idea of how things are organized in the memory. Here’s a resource to learn about various functions provided by the “string” library.
https://www.cplusplus.com/reference/string/string/
Here are some examples showing how to work with string class.
1. Creating a string object and printing it out
#include <iostream>
#include <string>
using namespace std;
int main()
{
string string1 {"Hello world"};
cout << string1 << endl;
return 0;
}
2. Creating a string and finding the length
#include <iostream>
#include <string>
using namespace std;
int main()
{
string string1 {"Hello world"};
cout << string1 << endl;
cout << "Its size is " << size(string1) << endl;
return 0;
}
3. Taking a string as a User Input
#include <iostream>
#include <string>
using namespace std;
int main()
{
string string1;
cout << "Enter string" << endl;
getline(cin, string1);
cout << string1 << endl;
return 0;
}
Note: Note: You can try using the “cin” instead of the “getline()” function but you will not be able to enter white spaces because the “cin” object treats whitespaces as null characters.
Null termination character and its importance
The null termination character is used to terminate a string. Let us consider the following string:
#include <iostream>
using namespace std;
int main()
{
char string[8] {"Hello"};
cout << string << endl;
return 0;
}
The representation of this string in the memory will be

It has 8 contiguous units of memory but we only have filled 5 if the “\0” isn’t present in the 6th unit then the compiler will go on reading the 7th and 8th elements that would have arbitrary values and we don’t want arbitrary values in our programs. Hence null character is an indication for the compiler to know when to terminate reading a string.
Conclusion
Strings are a confusing topic for absolute C++ beginners I hope that this blog clears most of the doubts. I would love to have any recommendations and reviews in the comments down below.
If you are on your way to becoming a C++ Expert, you might find our site really helpful. Hit this link for more C++ related posts
Check it out, it’s Simply the Best Code!
After reading your blog a different kind of excitement is growing in me to do c++. Your blogs are super simplified and very easy to understand. Keep it up.
Thanks :))
nice