Working with Strings in C++
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
Syntax:
Syntax: String string name; String str; Char abc[6] = {'H', 'e', 'l', 'l', 'o', ''};
There are two ways to input the values in a string
- cin>>str;
- 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:
Best-suited C++ courses for you
Learn C++ with these high-rated online courses
String functions
String S1= “Naukri”;
String S2= ”Learning”;
String S3;
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
#include<iostream.h> #include<string.h> 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; s.push_back('i'); // Displaying the string after push_back cout << "The new string is :”<< s << endl; // Deleting the i from the end using pop_back s.pop_back(); // Displaying the string after pop_back cout << "After pop_back operation, the string is : "; cout << s << endl; return 0; } Output: 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
Endnotes
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
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