-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathstringManipulation.cpp
81 lines (63 loc) · 1.73 KB
/
stringManipulation.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
#include <iostream>
#include <string>
// Problem: Checking for Substring
bool isSubstring(const std::string& str, const std::string& sub) {
return str.find(sub) != std::string::npos;
}
int main() {
std::string str = "Hello, World!";
std::string sub = "World";
if (isSubstring(str, sub)) {
std::cout << "'" << sub << "' is a substring of '" << str << "'." << std::endl;
} else {
std::cout << "'" << sub << "' is not a substring of '" << str << "'." << std::endl;
}
return 0;
}
#include <iostream>
#include <string>
// Problem: Checking for Subsequence
bool isSubsequence(const std::string& str, const std::string& sub) {
int i = 0, j = 0;
while (i < str.length() && j < sub.length()) {
if (str[i] == sub[j]) {
j++;
}
i++;
}
return (j == sub.length());
}
int main() {
std::string str = "abcde";
std::string sub = "ace";
if (isSubsequence(str, sub)) {
std::cout << "'" << sub << "' is a subsequence of '" << str << "'." << std::endl;
} else {
std::cout << "'" << sub << "' is not a subsequence of '" << str << "'." << std::endl;
}
return 0;
}
#include <iostream>
#include <string>
// Problem: Checking for Palindrome
bool isPalindrome(const std::string& str) {
int start = 0;
int end = str.length() - 1;
while (start < end) {
if (str[start] != str[end]) {
return false;
}
start++;
end--;
}
return true;
}
int main() {
std::string str = "racecar";
if (isPalindrome(str)) {
std::cout << "'" << str << "' is a palindrome." << std::endl;
} else {
std::cout << "'" << str << "' is not a palindrome." << std::endl;
}
return 0;
}