Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 计算字符串数组中的字符数?_C_String - Fatal编程技术网

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

C 计算字符串数组中的字符数?,c,string,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] !=

给定一个字符串数组,例如

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;
}
int countChars(字符*数组[],int len)
{
整数总计=0,计数=0;
对于(int i=0;i
对于char*array[]如何实际用于访问的解释,我们将不胜感激。我相信它应该是指向字符串的指针数组。

    你必须增加索引来考虑每个字符。
大概是这样的:-

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

}
for(int i=0;i
  • 同时,在所有计算结束时重置
    计数
    或添加到
    总计
作为对第二个问题的回答:-


char*array[]
基本上是指一个数组指针指向每个指针 到初始化它时使用的字符串文本

因此,一旦您使用了
array[i]
,您现在应该认为它什么都不是 而不是指向字符串文字的指针


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

此外,如果函数具有返回类型
size\u t
size\u 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;
}
此数组的每个元素

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

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

Yes
char*数组[]={“aa”、“bb”、“cc”}
是指向字符串的指针数组

  • array[0]
    指向
    “aa”
  • array[1]
    指向
    “bb”
  • 数组[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
}
int countChars(字符*数组[],int len)
{
整数计数=0;
for(int-arrayindex=0;arrayindex

您可以使用
sizeof(示例)/sizeof(示例[0])
代替硬编码
3
,因为您的数组包含字符串常量,所以您应该使用
常量声明它:

const char *example[3];
如果您试图为示例[i][j]分配字符,如果没有
const
,编译器将不会警告您。出于同样的原因,形式参数也应该用
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(a)(sizeof(a)/sizeof(a)[0])
静态整数字符数(常量字符*字符串[],整数长度)
{
int结果,i,j;
结果=0;
对于(i=0;i

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

那么,您有什么问题?我看到的一个明显的错误是,您没有在每个字之间重置
计数
。似乎
if(array[I]!=NULL)
没有任何意义。如果数组中删除了字符串。如果您正在计算字节,这很简单。如果要计算字符数,它可能会涉及很多内容。(多字节编码,组合字符,…)
count
在if块中定义<代码>总数+=计数:在范围外使用
计数
。还包括终止characters@BLUEPIXY.:答案正确地检测到问题。但是谢谢你的帮助
#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)));
}