计算字符串数组中的字符数?

时间:2017-11-08 09:17:25

标签: c string

给定一系列字符串,例如......

char *example[] = {"s", "ss", "sss"};

如何编写一个函数来计算数组中包含终止字符的字符总数,而不使用strlen()等标准库。

跟随是我的尝试

int countChars(char *array[], int len)
{
    int total = 0, count = 0;


    for (int i = 0; i < len; i++)
    {
        if (array[i] != NULL)
        {
            while (*array[i] != '\0') {
                count++;
            }
            count++;
        }
        total += count;
    }

    return total;
}

关于char * array []如何实际适用于访问的解释将受到赞赏。我相信它应该是一个指向字符串的指针数组。

4 个答案:

答案 0 :(得分:2)

  • 您必须增加索引以考虑每个字符。

这样的事情: -

for (int i = 0; i < len; i++)
{
    if (array[i] != NULL)
    {
        int j=0,count=0;
        while (array[i][j++] != '\0') {
            count++;
        }
        total += count;     
    }

}
  • 同时重置count或在所有计算结束时添加到total

作为对第二个问题的回答: -

  

char* array[]基本上表示每个指向的数组指针   到你初始化它的字符串文字。

     

所以一旦你使用array[i],你现在应该认为它什么都不是   除了指向字符串文字的指针。

答案 1 :(得分:0)

您需要为for循环内的每个已处理字符串重新初始化变量count,并在while循环内增加表达式*array[i]

当函数具有返回类型size_t时更好(size_t是标准C函数strlen和运算符sizeof返回的类型)

该功能可以看起来像示范程序中所示。

#include <stdio.h>

size_t countChars( const char *array[], size_t n )
{
    size_t count = 0;

    while ( n-- )
    {
        if ( array[n] )
        {
            size_t i = 0;
            do { ++count; } while ( array[n][i++] );
        }
    }

    return count;
}

int main(void) 
{
    const char * example[] = { "s", "ss", "sss" };

    printf( "%zu\n", countChars( example, sizeof( example ) / sizeof( *example ) ) );

    return 0;
}

程序输出

9

此数组的每个元素

char *example[] = {"s", "ss", "sss"};

具有类型char *,并且是指向相应字符串文字的第一个字符的指针。

答案 2 :(得分:0)

由于您的数组包含字符串常量,因此应使用const声明它:

const char *example[3];

如果没有const,如果您尝试将字符分配给示例 [ i ] [ j >,编译器将不会发出警告]。出于同样的原因,形式参数也应该用const声明。

对于没有副作用的纯函数,最好将其命名为反映结果。因此我会使用 charCount 而不是 countChars (或者 totalLength )。焦点应该放在名词上(即 count length )。

这是我的解决方案:

#include <stdio.h>

#define LEN(a) (sizeof (a) / sizeof (a)[0])

static int CharCount(const char *strings[], int len)
{
    int result, i, j;

    result = 0;
    for (i = 0; i < len; i++) {
        j = 0;
        while (strings[i][j] != '\0') {
            result++;
            j++;
        }
    }
    return result;
}


int main(void)
{
    const char *strings[] = { "s", "ss", "sss" };

    printf("character count: %d\n", CharCount(strings, LEN(strings)));
}

长度宏 LEN 非常方便,是处理数组长度最不容易出错的方法。

答案 3 :(得分:-1)

char *array[] = {"aa", "bb", "cc"}是指向字符串的指针数组。

  • array[0]指向"aa"
  • array[1]指向"bb"
  • array[2]指向"cc"

你可能想要这个:

int countChars(char *array[], int len)
{
  int count = 0;

  for (int arrayindex = 0; arrayindex < len; arrayindex++)
  {
    const char *stringptr = array[arrayindex];
                                // stringptr will point successively
                                // to "s", to "ss" and to "sss"

    while (*stringptr++)
      count++;                  // increment count until NUL character encountered
    count++;                    // one more for NUL character
  }

  return count;
}

int main() {
  char *example[] = { "s", "ss", "sss" };

  int x = countChars(example, 3);   // x contains 9 after the call to countChars
                                    // that is 2 + 3 + 4
}

您可以使用3来代替硬编码sizeof(example) / sizeof(example[0])

相关问题