[圖解C] ex 6-6

// exercise 6-6: counting the strings length

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char name[][10]={"John", "Mary", "Wilson", "Candy", "Allen"};
    int i, j, count;
    char str[10];
   
    for (i=0 ; i<5 ; i++){
        count = 0;
       
        // clear the remain value of str
        for (j=0 ; j<10 ; j++){
            str[j] = '\0';
            str[j] = name[i][j];
        }
       
        // counting the lengthe
        for (j=0 ; j<10 ; j++){
            if (str[j] != '\0')
               count++;
        }
       
        printf("the length of %s is %d\n", str, count);
    }
   
    system("pause");
    return 0;
}