lundi 5 juillet 2021

Create a function that capitalizes the first letter of each word and transforms all other letters to lowercase

I have this exercise who capitalize the first character in given sentence.Please i can't understood what "i - 1" and str[i - 1] means

#include <stdio.h>
char* ft_strcapitalize(char *str)
{
    int i;

    i = 0;
    while (str[i])
    {
        if ((i - 1 == 0 && str[i] != ' ')
            || (str[i - 1] >= 'a' && str[i - 1] <= 'z')
            || (str[i - 1] >= 'A' && str[i - 1] <= 'Z')
            || (str[i - 1] >= '1' && str[i - 1] <= '9'))
        {
        }
        else if (str[i] >= 'a' && str[i] <= 'z')
                str[i] -= 32;
        i++;
    }
    return (str);
}

int main(void)
{
    char s[] = "salut, comment  tu vas ? 42mots quarante-deux; cinquante+et+un";
    ft_strcapitalize(s);
    printf("%s", s);
}

Aucun commentaire:

Enregistrer un commentaire