C++ Strings C-PLUS-PLUS
- C++ Introduction
- C++ Syntax
- C++ Output (cout <<)
- C++ Comments
- C++ Variables
- C++ User Input
- C++ Data Types
- C++ Operators
- C++ Strings
- C++ Math
- C++ Booleans
- C++ If Else
- C++ Switch
- C++ While Loop
- C++ Do/While Loop
- C++ For Loop
- C++ Break and Continue
- C++ Arrays
- C++ References
- C++ Pointers
- C++ Functions
- C++ Functions Parameters
- C++ Function Overloading
- C++ OOP
- C++ Classes and Objects
- C++ Class Methods
- C++ Constructors
- Destructors in C++
- C++ Access Specifiers
- C++ Encapsulation
- C++ Inheritance
- C++ Polymorphism
- C++ Files
- C++ Exception Handling
C++ Strings
C++ Strings
Strings are used for storing text.
A string
variable contains a collection of characters surrounded by double-quotes.
Example
Create a variable of type string
and assign it a value.
To use strings, you must include an additional header file in the source code, the <string>
library.
Example
#include <iostream>
#include <string>
using namespace std;int main() {
string greeting = "Hello";
cout << greeting;
return 0;
}
Output:
Hello
String Concatenation
The +
operator can be used between strings to add them together to make a new string. This is called concatenation.
If you will use +
operator with string then it concatenates it and if you use it with numeric/int/float then it adds variables/values.
Example
#include <iostream>
#include <string>
using namespace std;
int main () {
string firstName = "Deepak "; // Note: Hear is one space after Deepak
string lastName = "Chahar";
string fullName = firstName + lastName;
cout << fullName;
return 0;
}
Output:
Deepak Chahar
String Length
A string in C++ is actually an object, which contains functions that can perform certain operations on strings. For example, the length of a string can be found with the length()
function.
Example
#include <iostream>
#include <string>
using namespace std;int main() {
string txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
cout << "The length of the txt string is: " << txt.length();
return 0;
}
Output:
The length of the txt string is: 26
Access Strings
You can access the characters in a string by referring to its index number inside square brackets []
.
Example
#include <iostream>
#include <string>
using namespace std;int main() {
string myString = "Hello";
cout << myString[0];
cout << "\n";
cout << myString[1];
return 0;
}
Output:
H
e
Note: String indexes start with 0: [0] is the first character. [1] is the second character, etc.
Change String Characters
To change the value of a specific character in a string, refer to the index number, and use single quotes.
Example
#include <iostream>
#include <string>
using namespace std;int main() {
string myString = "Hello";
myString[0] = 'D';
cout << myString;
return 0;
}
Output:
Dello
User Input Strings
It is possible to use the extraction operator >>
on cin
to display a string entered by a user.
However, cin
considers a space (whitespace, tabs, etc) as a terminating character, which means that it can only display a single word (even if you type many words).
cout << "Type your full name: ";
cin >> fullName;
cout << "Your name is: " << fullName;
// Type your full name: Deepak Chahar
// Your name is: Deepak
From the example above, you would expect the program to print "Deepak Chahar", but it only prints "Deepak".
That's why, when working with strings, we often use the getline()
function to read a line of text. It takes cin
as the first parameter and the string variable as the second.
Example
#include <iostream>
#include <string>
using namespace std;int main() {
string fullName;
cout << "Type your full name: ";
getline (cin, fullName);
cout << "Your name is: " << fullName;
return 0;
}
Output:
Type your full name: [YOUR INPUT STRING] ex: Deepak Chahar
//OUTPUT: [ENTERED STRING] Deepak Chahar