# Week 7 - Maps
## Team
Team name: pils
Date: 15/07/2022
Members
Wouter Starrenburg
a couple of activities have been skipped because the exam is coming up and I'm very busy
## Activities
### Activity 1: The ctype header file
int isalpha(int c)
this function checks whether the passed character is alphabetic
int tolower(int c)
this function changes a letter to its' lowercase variant if it isn't already
### Activity 2: Count letters in a string
```c=
unsigned long countLetters(unsigned long counts[static 26], const char* str){
unsigned long count = 0;
for(int i = 0; str[i] != '\0'; i++){
if (isalpha(str[i])){
counts[tolower(str[i]) - 'a']++;
count++;
}
}
return count;
}
int main(void){
unsigned long counts[26] = {0};
const char* text = "The Quick Brown Fox Jumps Over The Lazy Dog.";
unsigned long total = countLetters(&counts[0], text);
assert(35 == total);
printCounts(&counts[0], false, true);
}
```
### Activity 3: Recognizing languages
```c=
const char* makeSignature(unsigned long counts[static 26]){
int highest_index = -1;
int prev_highest_index = -1;
unsigned long highest_val = 0;
static char signature[7];
signature[6] = '\0';
for (int j = 0; j < 26; ++j) { //slightly different first iteration
if(counts[j] > highest_val) {
highest_val = counts[j];
highest_index = j;
}
}
signature[0] = (char)highest_index + 'a';
prev_highest_index = highest_index;
for (int i = 1; i < 6; ++i) { //starts from second iteration
highest_val = 0;
for (int j = 0; j < 26; ++j) {
if(counts[j] > highest_val && j != prev_highest_index && counts[j] <= counts[prev_highest_index]) {
highest_val = counts[j];
highest_index = j;
}
}
signature[i] = (char)highest_index + 'a';
prev_highest_index = highest_index;
}
return &signature[0];
}
int main(void) {
unsigned long counts1[26] = {15,3,4,5,16,6,7,8,9,7,6,3,2,11,14,1,2,12,13};
unsigned long counts2[26] = {16,4,7,5,20,7,4,3,14,5,9,1,2,18,6,12,9,13,9,15};
assert(strcmp("eaosrn", makeSignature(counts1)) == 0);
assert(strcmp("enatir", makeSignature(counts2)) == 0);
}
```
some functions are not included in the LanguageRecognizer files and I do not have the time to go and find them, so I'm going to skip part 2.
| File | Signature | Language |
| ---------- | --------- | -------- |
| alice0.txt | | |
| alice1.txt | | |
| alice2.txt | | |
| alice3.txt | | |
### Activity 4: Find out: dictionaries around us
this is not important to me right now
### Activity 5: Find out: dictionaries in other languages
this is not important to me right now
### Activity 6: The ensureCapacity function
```c=
bool _ensureCapacity(counter_t* counter, const size_t minimumCapacity){
size_t new_capacity = counter->capacity;
while (minimumCapacity > new_capacity){
new_capacity = new_capacity * GROWTH_FACTOR + 1;
}
if (new_capacity > counter->capacity){
counter->data = realloc(counter->data, new_capacity);
counter->capacity = new_capacity;
}
return(counter->data != NULL);
}
```
### Activity 7: insertAt function
```c=
static bool _insertAt(counter_t* counter, const size_t index, const char* key, unsigned long value){
if (_ensureCapacity(counter, counter->size + 1)) {
memmove(&counter->data[index + 1], &counter->data[index], (counter->size - index)*sizeof(pair_t));
pair_t new_pair = makePair(key, value);
counter->data[index] = new_pair;
return true;
}
return false;
}
```
### Activity 8: Increment function
```c=
unsigned long increment(counter_t* counter, const char* key){
if(contains(counter, key)){
counter->data[indexOf(counter, key).index].value++;
return counter->data[indexOf(counter, key).index].value;
}
else if(insert(counter, key, 1)) return 1;
return 0;
}
```
### Activity 9: Find out: function `strtok`
strtok seems to take an input string and a string of separator characters, which it uses to tokenize the input string's first or last character. The tokenized character is then removed from the string, after which the strtok function can be called again on the same input string to take the next token out.
### Activity 10: How many words?
```c=
int main(void) {
counter_t c = processFile("alice0.txt");
printf("The words 'Alice', 'the' and 'rabbit' occur %lu, %lu and %lu times respectively.\n",
increment(&c, "Alice") - 1, increment(&c, "the") - 1,
increment(&c, "rabbit") - 1);
destroyCounter(&c);
}
```
after some hours of highly frustrating attempts at debugging and figuring it out, I give up and declare that my programming of the class functions is probably fine, however, all counts end up being reportedly 0. This is presumably because nothing is incremented, but I honestly don't see where it goes wrong.