vendredi 12 juin 2020

What does str[i - 1] == ' ' mean?

I've been reviewing a program that capitalises the first letter of every word in a string. For example, "every single day" becomes "Every Single Day".

I don't understand the part str[i - 1] == ' '. What does that do?

#include <stdio.h>

char    *ft_strcapitalize(char *str)
{
    int i;

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

int   main(void)
{
  char str[] = "asdf qWeRtY ZXCV 100TIS";

  printf("\n%s", ft_strcapitalize(str));
  return (0);
}

Aucun commentaire:

Enregistrer un commentaire