-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_basic.cpp
More file actions
106 lines (82 loc) · 2.04 KB
/
string_basic.cpp
File metadata and controls
106 lines (82 loc) · 2.04 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
101
102
103
104
105
106
#include <iostream>
using namespace std;
int findLength(const char* str) {
int length = 0;
while (str[length] != '\0') {
length++;
}
return length;
}
void copyString(char* S1, const char* S2) {
int i = 0;
while (S2[i] != '\0') {
S1[i] = S2[i];
i++;
}
S1[i] = '\0';
}
void concatenateStrings(char* S1, const char* S2) {
int i = 0;
while (S1[i] != '\0') {
i++;
}
int j = 0;
while (S2[j] != '\0') {
S1[i] = S2[j];
i++;
j++;
}
S1[i] = '\0';
}
int compareStrings(const char* S1, const char* S2) {
int i = 0;
while (S1[i] != '\0' && S2[i] != '\0') {
if (S1[i] != S2[i]) {
return S1[i] - S2[i];
}
i++;
}
return S1[i] - S2[i];
}
void reverseString(char* str) {
int length = findLength(str);
int i = 0;
int j = length - 1;
while (i < j) {
// Swap characters
char temp = str[i];
str[i] = str[j];
str[j] = temp;
i++;
j--;
}
}
int main() {
char S1[100], S2[100];
cout << "Enter string S: ";
cin.getline(S1, 100);
cout << "Length of S: " << findLength(S1) << endl;
cout << "Enter string S2: ";
cin.getline(S2, 100);
copyString(S1, S2);
cout << "After copying, S1: " << S1 << endl;
cout << "Enter another string S2 to concatenate: ";
cin.getline(S2, 100);
concatenateStrings(S1, S2);
cout << "After concatenation, S1: " << S1 << endl;
cout << "Enter string S2 to compare with S1: ";
cin.getline(S2, 100);
int comparisonResult = compareStrings(S1, S2);
if (comparisonResult == 0) {
cout << "S1 and S2 are equal." << endl;
} else if (comparisonResult < 0) {
cout << "S1 is lexicographically smaller than S2." << endl;
} else {
cout << "S1 is lexicographically greater than S2." << endl;
}
cout << "Enter string to reverse: ";
cin.getline(S1, 100);
reverseString(S1);
cout << "Reversed string: " << S1 << endl;
return 0;
}