Strings in C++
What are Strings?
A string is a sequence of characters. In C++, you can use the string
class from the
<string> library.
Declaring a String
Syntax: string variable_name = "value";
#include <iostream>
#include <string>
using namespace std;
int main() {
string message = "Hello, World!";
cout << message;
return 0;
}
Using getline()
Syntax: getline(cin, variable_name);
Used to read an entire line, including spaces.
#include <iostream>
#include <string>
using namespace std;
int main() {
string fullName;
cout << "Enter your full name: ";
getline(cin, fullName);
cout << "Hello, " << fullName << "!";
return 0;
}
Using cin.ignore()
Syntax: cin.ignore();
Clears the input buffer, useful after using cin
before getline()
.
#include <iostream>
#include <string>
using namespace std;
int main() {
int age;
string name;
cout << "Enter your age: ";
cin >> age;
cin.ignore(); // Clears input buffer
cout << "Enter your name: ";
getline(cin, name);
cout << "Hello, " << name << "! You are " << age << " years old.";
return 0;
}
Finding String Length (length())
Syntax: string_variable.length();
Returns the number of characters in a string.
#include <iostream>
#include <string>
using namespace std;
int main() {
string text = "C++ is awesome!";
cout << "Length: " << text.length(); // Output: 15
return 0;
}
Getting Substrings (substr())
Syntax: string_variable.substr(start_index, length);
Extracts a portion of the string.
#include <iostream>
#include <string>
using namespace std;
int main() {
string text = "Programming";
cout << text.substr(0, 4); // Output: "Prog"
return 0;
}
Replacing Part of a String (replace())
Syntax: string_variable.replace(start_index, length, "new_value");
Replaces a section of a string with a new value.
#include <iostream>
#include <string>
using namespace std;
int main() {
string sentence = "I love Java!";
sentence.replace(7, 4, "C++");
cout << sentence; // Output: "I love C++!"
return 0;
}
Inserting into a String (insert())
Syntax: string_variable.insert(index, "text_to_insert");
Adds a substring at the specified index.
#include <iostream>
#include <string>
using namespace std;
int main() {
string word = "Hello!";
word.insert(5, " World");
cout << word; // Output: "Hello World!"
return 0;
}
Assigning a String (assign())
Syntax: string_variable.assign("new_value");
Assigns a new value to an existing string.
#include <iostream>
#include <string>
using namespace std;
int main() {
string text;
text.assign("New text assigned!");
cout << text;
return 0;
}
Using String Arrays
Syntax: string array_name[size] = {"value1", "value2", ...};
Creates an array of strings.
#include <iostream>
#include <string>
using namespace std;
int main() {
string fruits[3] = {"Apple", "Banana", "Cherry"};
cout << "Favorite fruit: " << fruits[1]; // Output: Banana
return 0;
}
Concatenating Strings (+ or append())
Syntax:
string new_string = string1 + string2;
string1.append(string2);
Combines two strings together.
#include <iostream>
#include <string>
using namespace std;
int main() {
string firstName = "John";
string lastName = "Doe";
// Using +
string fullName = firstName + " " + lastName;
cout << fullName << endl;
// Using append()
string greeting = "Hello, ";
greeting.append(firstName);
cout << greeting;
return 0;
}