-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrings.cpp
More file actions
106 lines (81 loc) · 3.7 KB
/
Copy pathstrings.cpp
File metadata and controls
106 lines (81 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <iostream>
#include <string> // Standard library for string operations
using namespace std;
int main() {
// ==========================================
// 1. STRING CONCATENATION (Combining Text)
// ==========================================
cout << "=== 1. STRING CONCATENATION ===" << endl;
string firstName = "John";
string lastName = "Doe";
// Method A: Using + operator (add " " for space)
string fullName1 = firstName + " " + lastName;
cout << "Using + operator : " << fullName1 << endl;
// Method B: Using append()
string fullName2 = "John ";
fullName2.append("Doe");
cout << "Using append() : " << fullName2 << endl;
// ==========================================
// 2. NUMBERS VS STRINGS
// ==========================================
cout << "\n=== 2. NUMBERS VS STRINGS ===" << endl;
int x1 = 10, y1 = 20;
int z1 = x1 + y1; // Math addition -> 30
cout << "Numbers (10 + 20) : " << z1 << endl;
string x2 = "10", y2 = "20";
string z2 = x2 + y2; // Text joining -> "1020"
cout << "Strings (\"10\" + \"20\"): " << z2 << endl;
// ==========================================
// 3. STRING LENGTH (length vs size)
// ==========================================
cout << "\n=== 3. STRING LENGTH ===" << endl;
string txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
// Both length() and size() return the exact same character count
cout << "Using length(): " << txt.length() << endl; // 26
cout << "Using size() : " << txt.size() << endl; // 26
// ==========================================
// 4. ACCESS & MODIFY CHARACTERS
// ==========================================
cout << "\n=== 4. ACCESS & MODIFY CHARACTERS ===" << endl;
string myString = "Hello";
// Index starts at 0 -> H(0), e(1), l(2), l(3), o(4)
cout << "First character [0] : " << myString[0] << endl; // 'H'
// Change a single character (Use single quotes ' ' for char)
myString[0] = 'J';
cout << "After change [0]='J': " << myString << endl; // "Jello"
// ==========================================
// 5. SPECIAL CHARACTERS (Escape Characters)
// ==========================================
cout << "\n=== 5. SPECIAL CHARACTERS ===" << endl;
// Use backslash (\) to print quotes or backslashes safely
string quote = "We are the so-called \"Vikings\" from the north.";
string singleQuote = "It\'s alright.";
string backslash = "The character \\ is called backslash.";
cout << quote << endl;
cout << singleQuote << endl;
cout << backslash << endl;
// ==========================================
// 6. USER INPUT (cin vs getline)
// ==========================================
cout << "\n=== 6. USER INPUT ===" << endl;
string fullName;
cout << "Enter your full name: ";
/*
KEY DIFFERENCE:
- cin >> name; -> Reads a SINGLE word (stops at space).
- getline(cin, name); -> Reads a FULL LINE (includes spaces).
PRO TIP: If 'getline()' is skipped after using 'cin >>',
just put 'cin.ignore();' right before 'getline()'.
*/
getline(cin, fullName);
cout << "Hello, " << fullName << "!" << endl;
// ==========================================
// 7. C-STYLE STRINGS (Legacy C Method)
// ==========================================
cout << "\n=== 7. C-STYLE STRINGS ===" << endl;
string regularString = "Hello"; // Modern C++ string (easy to modify)
char cStyleString[] = "Hello"; // Fixed-size character array from C language
cout << "Regular C++ String : " << regularString << endl;
cout << "C-Style String : " << cStyleString << endl;
return 0;
}