pointersampersandbus

C string capitalize problem. I encounter bus error


I am making capitalized function. But when execute this code, the bus error occur. Is there anyone can debug this? I really need your help!! please help me . I am computer programming novice without c knowledge.

#include <stdio.h>
void    up(char *c)
{
    if((*c < ‘z’) && (*c > ‘a’))
    {
        *c -= 32;
    }
}
char    *ft_strcapitalize(char *str)
{
    int i;
    i=0;
    while (str[i])
    {
            up(&str[i]);
        i++;
    }
    return str;
}
int main()
{
    char *str = “salut, comment tu vas ? 42mots quarante-deux cinquante”;
    ft_strcapitalize(str);
    printf(“%s”, str);
}

Solution

  • This solution should work for you.

    #include <stdio.h>
    
    void up(char *c)
    {
        if (!c)
            return;
        if((*c <= 'z') && (*c >= 'a'))
            *c -= 32;
    }
    
    char *ft_strcapitalize(char *str)
    {
        int i;
    
        for (i = 0; str[i]; i++)
            up(&str[i]);
        return str;
    }
    
    int main()
    {
        char str[] = "salut, comment tu vas ? 42mots quarante-deux cinquante";
    
        ft_strcapitalize(str);
        printf("%s\n", str);
        return 0;
    }