C - Finds the number of strings in a two-dimensional array and the length of each string:

Description

Finds the number of strings in a two-dimensional array and the length of each string:

Demo

#include <stdio.h>

int main(void)
{
  char str[][70] = {
    "test.",/* www .j ava  2  s  .  co m*/
    "asdf.",
    "test test.",
  };
  unsigned int count = 0;                              // Length of a string
  unsigned int strCount = sizeof(str) / sizeof(str[0]);  // Number of strings
  printf("There are %u strings.\n", strCount);

  // find the lengths of the strings
  for (unsigned int i = 0; i < strCount; ++i)
  {
    count = 0;
    while (str[i][count])
      ++count;

    printf("The string:\n    \"%s\"\n contains %u characters.\n", str[i], count);
  }
  return 0;
}

Result

The second index value has to be sufficient to accommodate the number of characters in the longest string, plus 1 for \0 at the end.

The string lengths are calculated in a nested loop:

for(unsigned int i = 0 ; i < strCount ; ++i)
{
      count = 0;
      while (str[i][count])
        ++count;

      printf("The string:\n\"%s\"\n contains %u characters.\n", str[i], count);
}