the code purpose is to check if the given word is in a correct suffixes. I wrote a function (small_letters_only) which should take the given word in "str" array and make it in the correct form which is only small letters. when im trying to change the big letters to small letters I get an "Unhandled exception thrown: write access violation." error. btw I cant change the char* str to char str, its a class work and they asked us to use char* str. thank you guys.
the full code:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
int main() {
int n = 0;
char* str = "comMunism!"; // this is the word that needs to be checked if its in the suffixes dict
int nouns_length = 6;
int verbs_length = 5;
int adjs_length = 6;
char* noun_suffixes[] = { "msi", "re", "sci", "ssen", "tnem", "tsi" };
char* verb_suffixes[] = { "de", "eta", "ezi", "gni", "yfi" };
char* adj_suffixes[] = { "elba", "evi", "hsi", "la", "luf", "suo" };
is_suffix_in_dict(str, noun_suffixes, nouns_length);
printf("Enter text to analyze:\n");
return 0;}
bool is_suffix_in_dict(char* str, char* dict[], int n){
small_letters_only(str);
int low = 0;
int mid, high = n - 1;
while (low <= high)
{
mid = (low + high) / DIVIDE;
int dict_length = strlen(dict[mid]);
int checks_equal = suffix_compare(str, dict[mid], dict_length);
if (checks_equal == 0)
return true;
else if (checks_equal == -1)
low = mid + 1;
else if (checks_equal == 1)
high = mid - 1;
}
return false;}
int suffix_compare(char* str, char* dict_mid, int dict_length){
int str_length = strlen(str) - 1;
int counter = 0;
for (int i = 0; i < (dict_length); i++)
{
if (str[str_length - counter] < dict_mid[counter])
{
return 1; // moves the binary search right.
}
if (str[str_length - counter] > dict_mid[counter])
{
return -1; // moves the binary search left.
}
if (str[str_length - counter] == dict_mid[counter])
{
counter++;
}
}
return 0;}
void small_letters_only(char* str){
int str_length = strlen(str);
for(int i = 0; i < str_length ; i++)
{
if (str[i] >= 'A' && str[i] <= 'Z')
{
str[i] += 32; // this is where the error occurs
}
if ((str[i] < 'A' || str[i] > 'Z') && (str[i] < 'a' || str[i] > 'z'))
{
int counter = i;
while (str[counter] != 0)
{
str[counter] = str[counter+1]; // I get the same error here.
counter++;
}
}
}
}