Different ways of summing string lengths

:: Programming

By: John Clements

Three different ways to sum string lengths:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
int numChars(char* word)
{
   int i, j, sum = 0;

   for(i = 0; word[i][j] != '\0'; i++)
   {
      for(j = 0; word[i][j] != '\0'; j++)
      {
         sum++;
      }
   }
   return sum;
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
int numChars(char * strings[], int len) {

  int sum = 0, i;

  for (i = 0; i < len; i++) {
    sum += strlen(strings[i]);
  }

  return sum;
}
1
2
3
4
5
int numChars(char * strings[], int len) {

  sumOf(lengths(strings));

}