lundi 25 septembre 2023

What is the best choice (while or for loop) for iteration through string's characters?

I am new to programing, and still learning the basics and figuring out best practices for C language. For iterating through string's characters i know of two ways, which is better and why?

int letters = 0, i = 0;
while (text\[i\] != '\\0')
{
if (isalpha(text\[i\]))
{
letters++;
}
i++;
int letters = 0, i = 0;
    while (text[i] != '\0')
    {
        if (isalpha(text[i]))
        {
            letters++;
        }
        i++;
    }

I tried both methods and both worked, but not sure which works better.

Aucun commentaire:

Enregistrer un commentaire