0
votes

I am trying to make a tolower function from scratch (for learning purposes) and keep getting this error even if I cast the correct types:

alternate.c:13:18: error: incompatible integer to pointer conversion passing 'int' to parameter of type 'const char *'; [-Werror]
                                strcat(temp, (char) i + 32);
                                             ^~~~~~~~~~~~~
/usr/include/string.h:136:72: note: passing argument to parameter '__src' here
extern char *strcat (char *__restrict __dest, __const char *__restrict __src)
                                                                       ^
alternate.c:15:19: error: incompatible integer to pointer conversion passing 'int' to parameter of type 'const char *'; [-Werror]
                                strcat(temp,  i);
                                              ^
/usr/include/string.h:136:72: note: passing argument to parameter '__src' here
extern char *strcat (char *__restrict __dest, __const char *__restrict __src)
                                                                       ^
alternate.c:18:17: error: incompatible integer to pointer conversion passing 'char' to parameter of type 'const char *'; take the address with & [-Werror]
                        strcat(temp, uppercase[i]);
                                     ^~~~~~~~~~~~
                                     &
/usr/include/string.h:136:72: note: passing argument to parameter '__src' here
extern char *strcat (char *__restrict __dest, __const char *__restrict __src)
                                                                       ^
3 errors generated.

This is the code that is causing trouble:

char * toLowerCase(char * uppercase){
    char * temp = "";
    for(int i =0; i<strlen(uppercase); i++){

        if( (int) uppercase[i] != 32){

            if( (int) uppercase[i] < 97){
                strcat(temp, (char) i + 32);
            }else{
                strcat(temp,  i);
            }
        }else{
            strcat(temp, uppercase[i]);
        }   
    }
    return temp;
}
1
char != char *. You strcat a string, not a single character. - Kevin
Your temp variable only has enough space to store a single character. You need more. Hint 1: at least strlen(uppercase) long. Hint 2: you cannot use a local variable, because you cannot return it. - Jongware
temp is a string literal, and you try to modify it by strcat - Yu Hao
sorry im new to c can you elaborate also how do you allocate memory? is it possible to append a char to a string? - Avi Mosseri
If in the C pleae read the manuals and ensure the datatypes match the signature. So when using an integer instead of a string expect something undefined. also learn to use a debugger. - Ed Heal

1 Answers

1
votes
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

char *toLowerCase(const char *uppercase){
    char *temp = malloc(strlen(uppercase)+1);

    if(temp){
        char *p = temp;
        while(*p++ = tolower(*uppercase++));
        /*
        while(*uppercase){
            *p++ = tolower(*uppercase++);
        }
        *p = '\0';
        */
    }
    return temp;
}

int main(void){
    char *str = toLowerCase("ABC-XYZ");
    printf("%s\n", str);//abc-xyz
    free(str);
    return 0;
}