C++ String Class and its Applications | Set 2

We have discussed String class and some of its functions in below set 1.

C++ string class and its applications | Set 1

More functions are discussed in this article

String vs Character Array
In C++, in addition to character array, there exist a similar kind of way to implement string, that is using string class which is a part of C++ standard library. To implement string using string class, we need to add header file . Basic difference between character array and a string is, in case of character array, the size has to be alloted at the time of declaration, i.e all memory once allocated is fixed and cannot be altered at run time. Whereas, for string, there is no need to specify the size and to allocate fixed memory at the time of declaration.





// C++ program to demonstrate Character Array
// and String
#include<string>// for string class
using namespace std;
int main()
    // Size has to be predefined in character array
    char str[80] = "GeeksforGeeks";
    // Size not predefined in string
    string s("GeeksforGeeks");
    // Printing character array and string
    cout << str << endl;
    cout << s << endl;
    return 0;



Some useful String Functions

This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

Improved By : thetmpguy

Article Tags :
Practice Tags :