Working with Strings in C++

Working with Strings in C++

3 mins read706 Views Comment
Updated on Jan 19, 2023 19:07 IST

Strings in C++ is a very simple yet very important topic from exams as well as interview point of view.


In this blog, we will cover string introduction with its declaration, string input functions, and the difference between string and character array.

What is a String?

A string is any collection of characters that are interpreted literally by a script. For example, “C++ strings” is an example of strings. The string can also contain one word or can have spaces and numbers in it. For example, the word “India” and the phrase “I visited India 3times” are both strings. Even “12345” could be considered a string if specified correctly. The thing to note here is that string is written always in quotation marks.

String declaration


String string name;
String str;
Char abc[6] = {'H', 'e', 'l', 'l', 'o', ''};

There are two ways to input the values in a string

  1. cin>>str;
  2. You can specify the value of the string while declaring it. Like

String str=” hello”;

     char abc[6] = {‘N’, ‘I’, ‘G’, ‘H’, ‘T’, ‘’};

The article you should read:

Classes and Objects in C++
Classes and Objects in C++
If you are a student then you must have got this question in the exam or practical viva and if you are a job seeker then you must have more

String functions

String S1= “Naukri”;

String S2= ”Learning”;

String S3;

Class Vs. Object: What are the Differences?
Class Vs. Object: What are the Differences?
class is a blueprint or template for creating objects in object-oriented programming and bjects are the instances of class. In this article we will expore Class Vs. Object. We more
Master Member Functions in C++
Master Member Functions in C++
If you are new to C++, it is crucial to learn about the Member function. With different types of member functions in C++ as well as the methods of more
Exception handling in C++
Exception handling in C++
Exception handling in C++ provides a way to deal with unexpected situations such as run-time errors. In this we have covered Exception handling in C++with programming explanation with code more

Difference between strings and character arrays

You must be thinking that both the character array and the C++ String class are used for string representation, what could be the difference? Let’s discuss some of the primary differences:

  • A string is a class that defines an object but a Character array is an array.
  • Character array size is allocated statically as well as allocated in advance. Hence, during run time extra memory is not available, and it wastes the memory that is not used. Since string size is allocated dynamically as well as not allocated in advance. So chances of memory wastage are very less there.
  • String doesn’t have the risk of array decay but Character array has the risk of array decay.
  • When it comes to implementation. Strings are a bit slower than character arrays.
  • In comparison to character array String class offers more in-built functions to work with and manipulate strings.

C++ String Input Functions

Function Description
getline() It is used to read as well as store a string entered by the user. You can enter a string having white spaces.
push_back() A new character is inputted at the string’s end.
pop_back() Deletes string’s last character

Example: Using C++ String Input Functions

int main(){
string s; // Declaring a string variable
cout << "Enter a string: ";   
getline(cin,s);    	// Using getline() to accept input
cout << "This is initial string” :<< s << endl;
// Displaying the string after push_back
cout << "The new string is :<< s << endl;
// Deleting the i from the end using pop_back
// Displaying the string after pop_back
cout << "After pop_back operation, the string is : ";
cout << s << endl;
return 0;
Enter a string:
Shiksha Online
This is initial string Shiksha Online
The new string is : Shiksha Onlinei
After pop_back operation, the string is : Shiksha Online 


In this blog, you learned about strings in C++ and their difference from character arrays. If you want to know about C++ classes then you can learn about them in this blog. If you liked this blog please share it with your friends also.

Top Trending Articles:
Data Analyst Interview Questions | Data Science Interview Questions | Machine Learning Applications | Big Data vs Machine Learning | Data Scientist vs Data Analyst | How to Become a Data Analyst | Data Science vs. Big Data vs. Data Analytics | What is Data Science | What is a Data Scientist | What is Data Analyst

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio