
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference Between String and Char Types in C++
In C++, char[] represents a character array, while string is a class in STL to manage text data. Both string and char[] can be used to store and process text data. But, they are different in terms of structure, usage, memory management, and capabilities. In this article, we will discuss the difference between string and char[] types in C++.
char[] in C++
char[] is an array of characters that is used to store a string. It is a C-style string representation. The size of char array will be fixed. At the end of the array, a null character '\0' will be added to indicate the end of the string. This is called null termination. If you are defining a string using char array, you have to manually manage the memory and null termination.
The syntax for declaring a character array is:
char str[] = "Hello";
Example
In the code below, we defined a char array greeting[] and initialized it with a string. Using the concept of null termination, we iterated through the array to printed each character of the string.
#include <iostream> #include <iostream> using namespace std; int main() { char greeting[] = "Hello"; cout << "Greeting: " << greeting << endl; cout << "Character at index 1: " << greeting[1]<< endl; // Loop will run until it reaches the null character of the string for(int i=0; greeting[i] != '\0'; i++){ cout << greeting[i]; } return 0; }
The output of the above code will be:
Greeting: Hello Character at index 1: e Hello
string in C++
The string is a class defined in the <string> header of C++ STL, that is used to define and manipulate text data inside C++ programs. The text data defined using string class is automatically managed by the C++ compiler. The string class also provides several built-in functions to manipulate the string data. This is more secure and easier to use than char arrays.
The code below shows how to define a string using the string class:
// Include the string library #include <string> // Define a string string str = "Hello";
Example
In the example code below, we used the string class to define a string. The length() function of string class is used to get the length of the string.
#include <iostream> #include <string> using namespace std; int main() { string greeting = "Hello"; cout << "Greeting: " << greeting << endl; cout << "Length: " << greeting.length() << endl; return 0; }
The output of the above code will be:
Greeting: Hello Length: 5
Difference between string and char[]
Here are the key differences between string and char[] in C++:
char[] | string |
---|---|
char[] is an array of characters. | string is a C++ class provided by the STL to define and manage text data. |
It requires manual handling of memory and null termination. | It manages memory dynamically and automatically handles null termination. |
Operations like copying, concatenation require functions from <cstring>. | String defined using string class supports operators like +, = and has built-in methods like append(), substr(), etc. |
Traditional approach, commonly used in C programming. | Modern approach, commonly used in C++ programming. |
Example:
char name[] = "Alice"; |
Example:
string name = "Alice"; |