Character Array Vs String Class in C++

Character Array and String Class are the two important topics of the C++ programming language and have their own use in many different situations. Both are used to deal with sequences of characters but many people get confused about finding the difference between them and when to use which one. 

String Class in C++ is introduced because there were many drawbacks to Character Array and you will be able to find it when you will read the difference between Character Array and String Class.

Character Array String Class
A character Array is an array used to store character-type data.

Ex: char ch[4] = "abc";
A string is a class in C++ programming and the string type variable is basically the object of the string class.

Ex: string str = "algolesson";
In character Array, we need to know the size of an array in advance. It string there is no need to know the size in advance. They adjust their size based on the requirement.
We cannot use C++ Operator on Character Array. We can use C++ Operator on String Class.
Operations like concatenation or append are difficult as a larger size Char array is required. An operation like concatenation or append are easier change in string size takes place automatically.
Character Arrays are much faster. Strings are slower as compared to the character array.

Example of Character Array code in C++:
//C++ example program for Character Array
#include<iostream>
#include<cctype>
using namespace std;

int main(){
  char ch[]{"Algolesson"};

  cout<<ch<<endl;
  ch[4] = 'L';
  for(int i = 0; i < 11; i++){
    cout<<ch[i];
  }
  return 0;
}
Output:
Algolesson
AlgoLesson


The drawback of Character Array.

The main drawback of the Character array is that it fails to operate with the standard operators of C++ and they are not dynamic in nature as their sizes are fixed in advance and cannot change at run time. 
char ch1[]{"algolesson"}; //OK
ch1 = "coding"; //error- not OK

char ch2[]{"welcome"};
ch2 = ch1 + ch2; //error - we cannot use operator
  • Once the Character array is initialized with a set of characters then it cannot be initialized again with a different set of characters.
  • We cannot use + operators on Character Arrays in C++.

Although these above drawbacks of character array is removed in string class where we can easily increase our string size at run time and we can use several operators on string for comparison and manipulation task. There are many string member functions available in which helps us to perform operations on strings.

Example of String Class in C++:
//C++ example program for String Class
#include<iostream>
#include<string>
using namespace std;

int main(){
  string str1 = "Welcome to ";
  string str2 = "Algolesson";

  cout<<str1<<endl;
  cout<<str2<<endl;
  cout<<"Size of str1: "<<str1.size()<<endl;
  str1 = str1 + str2; //concatenation
  cout<<str1<<endl;
  cout<<"Size of str1: "<<str1.size()<<endl;

  return 0;
}
Output:
Welcome to 
Algolesson
Size of str1: 11
Welcome to Algolesson
Size of str1: 21

In the above code, we can observe that we can easily use operators on string data type and the size of str1 get automatically change after concatenation operation. This is how string class help us to overcome from the drawbacks of Character Array.

Next:

⚡ Please share your valuable feedback and suggestion in the comment section below or you can send us an email on our offical email id ✉ algolesson@gmail.com. You can also support our work by buying a cup of coffee ☕ for us.

Similar Posts

No comments:

Post a Comment


CLOSE ADS
CLOSE ADS