-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ21.cpp
More file actions
100 lines (79 loc) · 2.12 KB
/
Q21.cpp
File metadata and controls
100 lines (79 loc) · 2.12 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
96
97
98
99
100
// 21. Write a program to convert a given string to uppercase.Example: Input: "hello", Output: "HELLO".
#include <iostream>
#include <cctype>
using namespace std;
int main() {
string input;
cout << "Enter a string: ";
getline(cin, input);
for (int i = 0; i < input.length(); i++) {
input[i] = toupper(input[i]);
}
cout << "Uppercase string: " << input << endl;
return 0;
}
#include <iostream>
#include <algorithm>
#include <locale>
using namespace std;
string toUpperCaseLocale(string str) {
locale loc;
transform(str.begin(), str.end(), str.begin(), [&loc](char c) { return toupper(c, loc); });
return str;
}
int main() {
string input = "hello";
cout << "Uppercase (Using locale): " << toUpperCaseLocale(input) << endl;
return 0;
}
#include <iostream>
using namespace std;
int findDifferenceSingleLoop(int arr[], int size) {
int maxElement = arr[0];
int minElement = arr[0];
for (int i = 1; i < size; i++) {
if (arr[i] > maxElement) {
maxElement = arr[i];
}
if (arr[i] < minElement) {
minElement = arr[i];
}
}
return maxElement - minElement;
}
int main() {
int arr[] = {80, 30, 70, 50, 20};
int size = sizeof(arr) / sizeof(arr[0]);
cout << "Difference (Single Loop): " << findDifferenceSingleLoop(arr, size) << endl;
return 0;
}
//
#include <iostream>
using namespace std;
string toUpperCaseASCII(string str) {
for (int i = 0; i < str.length(); i++) {
if (str[i] >= 'a' && str[i] <= 'z') {
str[i] = str[i] - 32;
}
}
return str;
}
int main() {
string input = "hello";
cout << "Uppercase (Brute Force): " << toUpperCaseASCII(input) << endl;
return 0;
}
#include <iostream>
#include <algorithm>
#include <locale>
using namespace std;
string toUpperCaseLocale(string str) {
locale loc;
transform(str.begin(), str.end(), str.begin(), [&loc](char c) { return toupper(c, loc); });
return str;
}
int main() {
string input = "hello";
cout << "Uppercase (Using locale): " << toUpperCaseLocale(input) << endl;
return 0;
}