While in the progress of making string functions, I have tried building a function somewhere similar to strlwr()
which I named lowercase()
:
#include <stdio.h>
#include <ctype.h>
char *lowercase(char *text);
int main() {
char *hello = "Hello, world!";
printf("%s\n", lowercase(hello));
}
char *lowercase(char *text) {
for (int i = 0; ; i++) {
if (isalpha(text[i])) {
(int) text[i] += ('a' - 'A');
continue;
} else if (text[i] == '\0') {
break;
}
}
return text;
}
I learned that the gap for a big letter and small letter would be 32, which is what I used. But then I got this error:
lowercase.c:14:13: error: assignment to cast is illegal, lvalue casts are not supported
(int) text[i] += 32;
^~~~~~~~~~~~~ ~~
I want to increment the value of the char if it is considered a letter from A-Z. Turns out I can't, since the char is in an array, and the way I'm doing it doesn't seem to make sense for the computer.
Q: What alternate ways can I use to complete this function? Can you explain further why this error is like this?