-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path28_CPP_Strings.cpp
More file actions
65 lines (53 loc) · 2.05 KB
/
28_CPP_Strings.cpp
File metadata and controls
65 lines (53 loc) · 2.05 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
#include <iostream>
#include <string> //Header file for using CPP strings
using namespace std;
int main(){
//A string variable contains a collection of characters (Sorting text into strings)
//Syntax: string identifier = "any_text";
string myString = "CPP Programmig";
cout << myString << endl;
//1)Using append function
string fName = "AAAA ";
string lName = "BBBB";
string fullName = fName.append(lName);
cout << fullName << endl;
//Concatenation of strings:
//1)Using + Operand
string A = "Introduction to";
string B = A + " " + myString;
cout << B << endl;
/*Note: Numbers are added by, and strings are concatenated by +
int x = 10, int y = 5 ==> (x + y) == 15 (integer)
string a = "10", string b = "5" ==> (a + b) == "105" (string)
but, (x + a) causes an error
*/
string myTxt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
//length of an string:
cout << myTxt.length() << ", " << myTxt.size() << endl; //size function is an alias of length function
//Characters of a string can be accessed by referring to its index like arrays
cout << myTxt[0] << ", " << myTxt[12] << endl;
//Specific characters in a string can be updated
myTxt[3] = '1';
myTxt[19] = '9';
cout << myTxt << endl;
//Updating characters using at() function
cout << myTxt.at(25) << endl;
myTxt.at(1) = 'b';
myTxt.at(25) = 'z';
cout << myTxt << endl;
//User input strings
string newTxt;
cin >> newTxt; //Every character before space or tab
//getline(cin, newTxt); //Space and tab can be used
cout << newTxt << endl;
/*
Common functions that are used with the string class:
string A = "..."
A.find("text") //finding text in A
A.append("text") //concatenating A with text
A.insert(x, "text") //inserting text into x position (index)
A.erase(x, y) //erasing y characters from y position (index)
A.replace(x, y, "text") //replacing y characters with text from x position (index)
A.compare(strVar) //comparing two strings
*/
}