Lesson 14
Strings
Covers string declare, input, length, concat, compare.
Lesson content
What are strings?
String stores text as a character sequence.
- "Hello"
- "C++ Programming"
- "John Doe"
Char holds one letter. String holds many.
Word on paper analogy. Each letter has an index.
H e l l o
0 1 2 3 4Why do strings exist?
- Reading names
- Displaying messages
- Comparing passwords
- Searching text
- Editing words and sentences
Real-world use cases
- Login systems
- Search engines
- Chat applications
- Text editors
- File processing
- Email validation
- Customer management systems
- Web applications
Including the string library
#include <string>Declaring a string
string variableName;
string name;Initializing a string
string language = "C++";Reading string input
cin stops at first whitespace.
cin >> name;
// Input: John Smith
// Output: Johngetline reads the whole line.
getline(cin, name);
// Input: John Smith
// Output: John SmithAccessing characters
name[0]
name[1]
name[2]String length
name.length()
name.size()Both return the character count.
Concatenating strings
+ operator joins two strings.
string fullName = firstName + " " + lastName;Comparing strings
if (password == "admin")Modifying strings
Change chars using their index.
name[0] = 'J';Example 1: Declare and display a string
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name = "Alice";
cout << name;
return 0;
}Example 2: Read a full name
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter your full name: ";
getline(cin, name);
cout << "Welcome, " << name;
return 0;
}Example 3: String concatenation
#include <iostream>
#include <string>
using namespace std;
int main()
{
string firstName = "John";
string lastName = "Doe";
string fullName = firstName + " " + lastName;
cout << fullName;
return 0;
}Example 4: Count vowels
#include <iostream>
#include <string>
using namespace std;
int main()
{
string text;
int vowels = 0;
cout << "Enter a word: ";
cin >> text;
for (int i = 0; i < text.length(); i++)
{
char ch = tolower(text[i]);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
{
vowels++;
}
}
cout << "Number of vowels = " << vowels;
return 0;
}Example 5: Password verification
#include <iostream>
#include <string>
using namespace std;
int main()
{
string password;
cout << "Enter password: ";
cin >> password;
if (password == "admin123")
{
cout << "Access Granted";
}
else
{
cout << "Access Denied";
}
return 0;
}Common mistakes and pitfalls
- cin for full names. Fix: use getline
- Missing string header. Fix: include it
- Invalid index access. Fix: check length first
- Comparing C-strings with ==. Fix: use std::string or strcmp
- Mixing cin and getline. Fix: use cin.ignore() first
// Wrong
string name;
cin >> name;
// Input: John Smith
// Output: John// Correct
string name;
getline(cin, name);
// Output: John SmithBest practices
- Prefer std::string over char arrays
- Use getline for names with spaces
- Validate input before processing
- Avoid accessing past string length
- Use clear names like userName
- Use built-in string functions first
When not to use this
- Legacy C libraries need char arrays
- Memory-constrained embedded systems
- Fixed-size binary data, not text
Summary
- Strings store and manage text
- Include string header for std::string
- Strings declare, init, modify, compare easily
- cin stops at space, getline reads full line
- Characters use zero-based indexing
- length or size gives character count
- + operator joins strings
- Prefer std::string in modern code
Frequently asked questions
What is a string in C++?
Character sequence storing text data.
What is the difference between char and string?
Char holds one letter. String holds many.
Why does cin stop reading at spaces?
>> treats space as separator. Use getline instead.
What is the difference between length() and size()?
No real difference. Both count characters.
Why use std::string over a character array?
Safer, easier, auto memory, built-in operations.