-
Notifications
You must be signed in to change notification settings - Fork 0
/
strings.cpp
118 lines (96 loc) · 2.79 KB
/
strings.cpp
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
107
108
109
110
111
112
113
114
115
116
117
118
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
/*
Character arrays:
- Need to know size beforehand.
- Larger size required for operations
- No terminating extra character
Strings:
- No need to know size beforehand
- Performing operations like concat is easy.
- Terminate with a special character '\0'
*/
int main()
{
//* types of string declarations
string str;
string str1(5, 'n'); // string of length 5 with each character as 'n'
string str2 = "Kanav Arora";
//* getline() function
string str3;
// getline(cin, str3);
//* append() function
string str4 = "fam";
string str5 = "ily";
str4.append(str5);
cout<<str4<<endl;
//* other way
string str6 = "fam";
string str7 = "ily";
cout<<str6 + str7<<endl;
//* String indexing
string str8 = "Kanav";
cout<<str8[1]<<endl;
//* clear() function
string str9 = "ndwbdhsnscbscdscbscs";
str9.clear();
//* compare strings
/*
int y = a.compare(b)
if y>0:
a>b
if y=0:
a==b
if y<0:
a<b
*/
string str10 = "abc";
string str11 = "xyz";
cout<< str11.compare(str10)<<endl;
//* empty() function
string str12 = "abc";
str12.clear();
cout<< str12.empty()<<endl;
//* erase() function - delete characters from string
string str13 = "abcdefg";
str13.erase(3,3); // erase(starting_index, number of characters to be removed)
//* find() function - return index of first occurrence
string str14 = "abcdefg";
cout<<str14.find("def")<<endl;
//* insert() function
string str15 = "anav";
str15.insert(0,"k");
//* length() function
cout<<str15.size()<<endl;
cout<<str15.length()<<endl;
//* substr() function - substring function
string str16 = str15.substr(1,4);
cout<<str16<<endl;
//* stoi() function - string to integer
string str17 = "789";
int x = stoi(str17);
cout<<x<<endl;
//* to_string() function
int y = 789;
string str18 = to_string(y);
cout<<str18<<endl;
//* sort string - sorting it lexicographically
string str19 = "mndsxbbx";
sort(str19.begin(), str19.end());
cout<<str19<<endl;
//* to upper case
string str20 = "abcdefghi";
transform(str20.begin(), str20.end(), str20.begin(), ::toupper);
cout<<str20<<endl;
//* to lower case
string str21 = "ABCDEFGHI";
transform(str21.begin(), str21.end(), str21.begin(), ::tolower);
cout<<str21<<endl;
//* sort in descending order
string str22 = "1 2 3 4 5";
sort(str22.begin(), str22.end(), greater<int>());
cout<<str22<<endl;
return 0;
}