-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ26.cpp
More file actions
95 lines (71 loc) · 1.72 KB
/
Q26.cpp
File metadata and controls
95 lines (71 loc) · 1.72 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
// 26. Write a program to check if a string ends with a specific character.Example: Input: ("codinggita", "a"), Output: true.
#include <iostream>
using namespace std;
bool checkChar(string str, char c) {
if (str[str.size() - 1] == c) {
return true;
}
return false;
}
bool checkChar1(string str, char c) {
return !str.empty() && str.back() == c;
}
int main() {
string str = "codinggita";
if (checkChar(str, 'a')) {
cout << "true" << endl;
} else {
cout << "false" << endl;
}
return 0;
}
//
#include <iostream>
using namespace std;
bool endsWithBruteForce(string str, char ch) {
if (str.length() == 0) {
return false;
}
if (str[str.length() - 1] == ch) {
return true;
}
return false;
}
int main() {
string inputString = "codinggita";
char targetChar = 'a';
if (endsWithBruteForce(inputString, targetChar)) {
cout << "Output: true" << endl;
} else {
cout << "Output: false" << endl;
}
return 0;
}
//
#include <iostream>
using namespace std;
bool endsWithEasy(string str, char ch) {
return !str.empty() && str.back() == ch;
}
int main() {
string inputString = "codinggita";
char targetChar = 'a';
if (endsWithEasy(inputString, targetChar)) {
cout << "Output: true" << endl;
} else {
cout << "Output: false" << endl;
}
return 0;
}
//
#include <iostream>
using namespace std;
bool endsWithOptimal(string str, char ch) {
return !str.empty() && str.back() == ch;
}
int main() {
string inputString = "codinggita";
char targetChar = 'a';
cout << (endsWithOptimal(inputString, targetChar) ? "Output: true" : "Output: false") << endl;
return 0;
}