-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathq1.2.cpp
More file actions
65 lines (56 loc) · 1.78 KB
/
q1.2.cpp
File metadata and controls
65 lines (56 loc) · 1.78 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
#include <assert.h>
#include <ctype.h>
#include <iostream>
#include <string.h>
#include <vector>
using namespace std;
void character_frequency(char *input_string, unsigned int *counter,
vector<int> *non_zero_indices) {
int string_length = strlen(input_string);
char c;
int index;
for (int i = 0; i < string_length; i++) {
c = toupper(input_string[i]);
index = (int)c;
if (*(counter + index) == 0)
non_zero_indices->push_back(index);
(*(counter + index))++;
}
}
int main(int argc, char **argv) {
assert(argc > 1);
vector<int> non_zero_indices1, non_zero_indices2;
unsigned int counter1[127] = {0}, counter2[127] = {0};
char *input_string1 = argv[1];
char *input_string2 = argv[2];
character_frequency(input_string1, counter1, &non_zero_indices1);
character_frequency(input_string2, counter2, &non_zero_indices2);
if (non_zero_indices1.size() != non_zero_indices2.size())
goto NEGATIVE;
else {
for (auto it = non_zero_indices1.begin(); it != non_zero_indices1.end();
it++) {
if (counter1[*it] != counter2[*it])
goto NEGATIVE;
}
goto POSITIVE;
}
NEGATIVE : {
cout << "both the strings ARE NOT permutations of each other\n";
goto PRINT_FREQUENCIES;
}
POSITIVE : { cout << "both the strings ARE permuations of each other\n"; }
PRINT_FREQUENCIES : {
cout << "printing characters and their frequencies for string1\n";
for (auto it = non_zero_indices1.begin(); it != non_zero_indices1.end();
it++) {
cout << char(*it) << ": " << counter1[*it] << '\n';
}
cout << "printing characters and their frequencies for string2\n";
for (auto it = non_zero_indices2.begin(); it != non_zero_indices2.end();
it++) {
cout << char(*it) << ": " << counter2[*it] << '\n';
}
}
return 0;
}