-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring.cpp
86 lines (66 loc) · 1.39 KB
/
string.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
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main(){
string str="ambar";
cout<<str<<endl;
//printing n times character
string str1(3,'n');
cout<<str1<<endl;
//sentence with spaces
//string str2;
//getline(cin,str2);
//cout<<str2<<endl;
//appending two string
string str3="fam";
string str4="ily";
cout<<str3+str4<<endl;
cout<<str3.append(str4)<<endl;
//accessing character of string
cout<<str3[2]<<endl;
//for clearing the string
string abc="hello india";
abc.clear();
//comparing two string
string s1="abc";
string s2="xyz";
cout<<s1.compare(s1)<<endl;
//to check string is empty or not
string s3="abc";
if(!s3.empty())
cout<<"string is empty"<<endl;
//erase function
string s4="nincompoop";
s4.erase(0,3);
cout<<s4<<endl;
//finding position in string(substring)
string s5="nincompoop";
cout<<s5.find("com")<<endl;
//insert function
string s6="lol";
cout<<s6.insert(2,"mc")<<endl;
//legth function
cout<<s6.size()<<endl;
//iterating in string
for (int i = 0; i < s6.size(); i++)
{
cout<<s6[i]<<endl;
}
//getting substring
string s7=s5.substr(6,2);
cout<<s7<<endl;
//string to integer
string s8="786";
int x= stoi(s8);
cout<<x+2<<endl;
//transform integer to string
int y=783;
cout<<to_string(x)+"2"<<endl;
//sorting string
//include algorithm header file
string s10="loveindia";
sort(s10.begin(), s10.end());
cout<<s10<<endl;
return 0;
}