-
Notifications
You must be signed in to change notification settings - Fork 0
/
DAY9.cpp
98 lines (83 loc) · 1.65 KB
/
DAY9.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
//sort and reverse function
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int arr[] = {5,2,4};
sort(arr,arr+3);
reverse(arr,arr+3);
for(auto a:arr)
{
cout<<a<<" ";
}
cout<<endl;
return 0;
}
//string array sorting
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
string str[] = {"Apple","Hat","Car","Banana"};
for(auto a:str)
{
cout<<a<<" ";
}
sort(str,str+4);
for(auto a:str)
{
cout<<a<<" ";
}
}
//Finding elements in string
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str = "Hello World, testing test string";
string strToFind = "test";
//cout<<string::npos;
size_t index = str.find(strToFind);
cout<<index;
cout<<endl;
index = str.find(strToFind , index+1);
cout<<index;
cout<<endl;
for(int i=index; i<index + strToFind.length(); i++)
{
cout<<str[i];
}
cout<<endl;
index = str.find(strToFind , index+1);
cout<<index;
cout<<endl;
return 0;
}
//string comparison
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
bool stringCompare(string s1,string s2)
{
if(s1.length()!=s2.length())
{
return s1.length()>s2.length();
}
else{
return s1<s2;
}
}
int main()
{
string str[]={"akhil","umang","vishal","tiya","varun","somya"};
sort(str,str+6,stringCompare);
for(auto ele:str)
{
cout<<ele<<" ";
}
return 0;
}