-1

i wanted to know why i can't transform all my letter into lowercase in my string in my function. I try to print the string before the "if" and it worked so i dont understand why i cannot do :

string[i] = string[i] - 'A' + 'a';

Here my function :

bool is_string_in_array(char * strings[], int n, char * string) {


for (int i=0;i<strlen(string);i++) {
        printf("%c\n",string[i]);
    if (string[i] >= 'A' && string[i] <= 'Z') {
        string[i] = string[i] - 'A' + 'a';

    }
}
for (int i = 0; i < n; i++) {
    if (strcmp(strings[i], string) == 0){
        return true;
    }
}
return false;

}

And i can not use tolower() function ... and i called with :

char *test ={"DOG"};
is_string_in_array(banned_words,n,test);

Thank you for your time

1 Answers1

1
#include <stdio.h>
#include <stdlib.h>
void low_case_string(char * string);
int main()
{
    char str[]="TESTING";
    low_case_string(str);
    return 0;
}
void low_case_string(char * string)
{

    for (int i=0; i<strlen(string); i++)
    {
        if (string[i] >= 'A' && string[i] <= 'Z')
        {
            string[i] = string[i] - 'A' + 'a';

        }
    printf("%c\n",string[i]);
    }
}

It does work the way you have writen your code, but you printed out the result before there would have been any change. I am not sure if this is what you were asked for

amibira
  • 21
  • 3