Character Arrays Vs Strings in C++

Character Arrays Vs Strings in C++

ยท

4 min read

Have you ever wondered why does character array even exist when we have strings to do a similar job? Well, I will talk about it in this blog, so let's get started.

Note - This is my very first blog and it would be great to have feedbacks.

Before discussing differences and similarities between the two, let's first get introduced to what are character arrays and strings and how they behave in C++.

Character Arrays

In C++, an array data structure can store elements of any primitive type, int, bool, float, double, or char. Thus, an array containing its elements as char is called a character array.

Char arrays are also often known as C-strings because of their unique behavior of allocation and reading.

Declaration of char array

char variableName [] = "value";

char name[] = "Shivansh";

Alternate ways of Declaration

char str[5] = "Shiv";

char str[] = {'S','h','i','v','\0'};

char str[5] = {'S','h','i','v','\0'};

We can clearly see a different pattern of declaring a char array, we have to store a '\0' character at the end of the array.

This is because we need to explicitly tell the C++ compiler where our array ends while taking a sentence as a string input.

It is also not necessary to use all the allocated space for the char array as it is for the other arrays, for example

char [500] = "Shivansh";

Taking Input in a char array

For a single word, we can directly take user input like,

char name[20];
cin >> name;

For a multi-word sentence, we have to use the function getline() which is defined in the string header file, for example

char yourName[100];
getline(cin, yourName);  
// can take multi-word input like "My name is Shivansh"

Printing the output in a char array

Unlike common arrays where we have to iterate over the whole array and print out the elements one by one, we can directly print out the whole char array at once.

cout << yourName; 
// "My name is Shivansh" will be printed out.

However, we can also print out the elements by iterating over them.

So far, we have seen how char array or C-strings works in C++, now let's look at strings in C++.

Strings in C++

A string is not a primitive datatype in C++. It is a class, defined in the string header file.

Declaration of a string

string variableName = "value";

string name = "Shivansh";

A string by default stores a special terminating character '\0'

Taking Input in a string

Similar to a char array, we can take input in two ways,

For a single word,

string name;
cin >> name;

For a multi-word sentence,

string yourName;
getline(cin, yourName);  
// can take multi-word input like "My name is Shivansh"

Printing the output in a string

It also prints the output similar to a char array,

cout << yourName; 
// "My name is Shivansh" will be printed out.

Now we have gained much-required knowledge of how strings work in C++, and we have noticed that we can almost do everything with char arrays that we can do with the strings, then what is the need for strings in C++?

Basically, a one-line answer would be, a string class is an implementation of char arrays in C++ and has been developed for ease of work.

But the main value that makes a difference between a char array and a string is Memory management.

As we know that arrays in C++ are Static in nature. We need to allocate the fixed size of the array at the time of declaration. But strings are Dynamically allocated so we do not need to worry about the extra space.

Some other features of using String class over Native C-strings or char arrays are -

1) So many pre-defined functions available in string class that are not available in char arrays like, append, length, find, replace, concat, etc.

2) Any standard C++ operator can be directly applied to the strings and not to char arrays.

3) We do not need to explicitly set a terminating char '\0'.

One major usage of char arrays over strings is that they can be accessed faster than strings.

Conclusion

Language C natively uses C-strings and that feature also comes along with all other features to C++. However, C++ developed its own string class to handle the strings. So it may be concluded that even both are much similar but C++ string class possesses more flexible usage and much better memory management.

Thank you for reading out my blog ๐Ÿ˜Š

I hope you liked it and found it helpful.

Do Comment to point out my mistakes.

Cover:- Rajat Gour

Connect with me on Twitter or LinkedIn