C++ mein strings kaafi important topic hai jab aapko characters ya text handle karna hota hai. C++ do tarah se string ko handle karta hai:
- Character arrays (C-style strings)
string
class (C++ STL)
1. Character Array (C-style Strings)
Sabse pehle traditional C-style strings dekhte hain jisme characters ek array ke form mein store hote hain, aur string ka end null character se hota hai.
Declaration & Initialization
char name[] = {'B', 'E', 'Y', 'O', 'N', 'D', ' ', 'M', 'A', 'N'};
Ya phir shortcut:
char name[] = "Beyond Man";
"Beyond Man"
ke andar last mein automatically\0
add hota hai.
2. Input and Output in Character Arrays
char name[20];
cin >> name; // space tak ka input lega
cout << name;
Limitation: cin
sirf space tak ka text read karta hai. Poora sentence lena ho toh cin.getline()
use karte hain.
cin.getline(name, 20);
3. String Functions (C-style)
strlen(str); // length
strcpy(dest, src); // copy
strcmp(str1, str2); // compare
strcat(str1, str2); // concatenate
4. C++ String Class (STL)
Modern C++ mein string
class use karna jyada easy aur safe hota hai. Ye class #include <string>
se aati hai.
Declaration:
string name = "Beyond Man";
Input & Output:
string fullName;
getline(cin, fullName); // entire line input
cout << fullName;
5. Common Operations on Strings
string a = "Hello";
string b = "World";
string c = a + " " + b; // Concatenation
cout << c; // Output: Hello World
cout << a.length(); // Length of string
cout << a[1]; // Access character: 'e'
6. Useful String Functions (C++)
Function Purpose
s.length()
Length of string
s.empty()
Check if string is empty
s.append("xyz")
Add to end
s.substr(0, 5)
Substring from index 0 of length 5
s.find("lo")
Find substring, returns index
s.compare(s2)
Compare two strings
s.erase(0, 2)
Erase part of string
7. Example Programs
Program 1: Reverse a String
#include <iostream>
#include <algorithm> // Required for reverse()
#include <string> // Required for using string class
using namespace std;
int main() {
string str = "Beyond Man";
// Reversing the string using reverse() from <algorithm>
reverse(str.begin(), str.end());
// Output the reversed string
cout << "Reversed string: " << str << endl;
return 0;
}
Output:
Reversed string: naM dnoyeB
Program 2: Check if a String is Palindrome
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main() {
string str;
cout << "Enter a string: ";
cin >> str; // Takes input without spaces
// Create a copy and reverse it
string rev = str;
reverse(rev.begin(), rev.end());
if (str == rev) {
cout << "Palindrome" << endl;
} else {
cout << "Not Palindrome" << endl;
}
return 0;
}
Output:
Enter a string: madam
Palindrome
Program 3: Count Vowels in a String
#include <iostream>
#include <string>
using namespace std;
int main() {
string s;
cout << "Enter a string: ";
getline(cin, s); // Accepts the whole line including spaces
int count = 0;
for (char c : s) {
c = tolower(c); // Convert character to lowercase for comparison
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
count++;
}
}
cout << "Number of vowels: " << count << endl;
return 0;
}
Output:
Enter a string: Hello World
Number of vowels: 3
Practice Questions
- Ek string lo aur count karo kitne capital letters hain.
- Ek string lo aur har word ko nayi line mein print karo.
- Do strings input lo aur check karo kya wo anagrams hain.
- Ek string input lo aur vowel ko
*
se replace karo.
Comments