Initialization of Strings
We can declare strings with the use of the following syntax:
Syntax:
string variable_name;
Example:
string hello; //empty string
string name=”LearningStrings”;
string greeting = “GoodMorning”;
Also check out - Substr C++
Functions for String Manipulation
Several functions are available for the manipulation of strings in C++. Let us start learning about these functions, which will help us.
Some essential functions to deal with strings are as follows:
-
getline(): We can take input of the strings using the function. It takes in and stores all the input streams until a new line comes in the way.
Syntax for use: getline (cin,string_varaible_name);
-
Push_back(): It pushes a specific character in a string to the end.
Syntax for use: stringVariable.push_back(‘a’);
-
front(): provides the character at the start of the string
Syntax for use: stringVariable.front();
-
back(): provides the character at the end of the string.
Syntax for use: stringVaraible.back();
After learning about these basic functions, let us learn how to use them in our code.
Example 1
#include <string.h>
#include <iostream>
using namespace std;
int main()
{
// Initialising two string varaibles
string string1, string2;
// taking one word as string input in a string variable
cin >> string1;
// taking one word as string input in a string variable
cin >> string2;
// declaration of two character type variables
char f, b;
// finding and storing first character of string
cout<<string1<<" "<<string2<<endl;
f = string1.front();
b = string2.front();
cout << f << endl;
cout << b << endl;
// using push_back() function to push back a certain character in the string
string2.push_back('!');
// Printing modified string2
cout << string1 << " " << string2 << endl;
return 0;
}
Input :
Coding
Ninjas
Output :
C
N
Coding Ninjas!
Some other functions that deal with the strings are as follows:
-
length(): used to return the value of the length of the string defined.
Syntax for use: int l= string_variable.length();// The integer l stores the value of the length of the string.
-
size (): used to get the value of the length of the string.
-
begin(): It gives us the reference of the first character.
-
end(): It provides us with the reference of the last character.
-
clear(): It deletes the contents of the string.
-
capacity(): It gives the memory storage size allocated for the string.
Syntax for use: string_variable.capacity();
-
shrink_to_fit(): To save memory used in the program, we decrease the capacity of
the string to its size so that no extra memory is utilized.
Syntax for use: string_varaible.shrink_to_fit();
The functions begin(), end() are used in the string interaction process. A code block that illustrates this is as follows:
Example 2
#include <bits/stdc++.h>
using namespace std;
int main()
{
// Initialisation of a string
string fun1 = "let us start string manipulation";
// Declaration of iterator through the string:
string::iterator str;
// printing out the string character
// use of for loop to traverse through the start to the end of the loop
for (str = fun1.begin(); str != fun1.end(); str++)
{
cout << *str;
}
// erasing the content of the string variable;
fun1.clear();
return 0;
}
Output:
let us start string manipulation
Try and compile with online c++ compiler.
FAQs
-
What is the difference between an array and a string?
The main difference is that an array is a data structure while a string is an object. The array can hold any data type, while strings only have character data types. Arrays can be changed, which means they are mutable while strings are not.
-
What is the difference between the char[] and string data types in C++?
A char array is just an array of characters, whereas a string is a class that contains a char array, but it automatically manages it.
-
How do we compare strings in C++?
Using std::string::compare(). It compares the value of the given string object (or a substring of the given string) to the sequence of characters that its arguments have specified.
Key Takeaways:
This blog begins with the introduction of a new data type- string. We understand an array of characters. Then we dive into several concepts of string. We learned how to declare and initialize strings. Further, we learned about several functions dealing with the string data types. For learning more such programming related concepts, visit here
Check out this article - C++ String Concatenation
Recommended problems -