Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.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_Arrays_String - Fatal编程技术网

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

C-计算字符串数组中的字符数,c,arrays,string,C,Arrays,String,我一直在一个项目中工作,以完成欧拉项目的17题,我试着用C语言来完成。 问题: 如果数字1到5是用文字写出来的:一、二、三、四、五,那么总共使用了3+3+5+4+4=19个字母 如果所有从1到1000(一千)的数字都是用文字写出来的,那么会使用多少个字母 我编写了一个函数,将1到1000之间的所有数字放入一个包含1001个元素的数组中(对于迭代,最后一个元素为NULL)。 但是当我试图计算字符串中每个元素的字符数时,我遇到了麻烦,因为我不知道如何计算。有人能帮我一点忙吗?假设您的数组名为arra

我一直在一个项目中工作,以完成欧拉项目的17题,我试着用C语言来完成。 问题:

如果数字1到5是用文字写出来的:一、二、三、四、五,那么总共使用了3+3+5+4+4=19个字母

如果所有从1到1000(一千)的数字都是用文字写出来的,那么会使用多少个字母

我编写了一个函数,将1到1000之间的所有数字放入一个包含1001个元素的数组中(对于迭代,最后一个元素为NULL)。
但是当我试图计算字符串中每个元素的字符数时,我遇到了麻烦,因为我不知道如何计算。有人能帮我一点忙吗?

假设您的数组名为
array

int count = 0, i;

for (i = 1; i <= 1000; ++i) {
    count += strlen(array[i]);
}
int count=0,i;

对于(i=1;i假设您的数组被称为
array

int count = 0, i;

for (i = 1; i <= 1000; ++i) {
    count += strlen(array[i]);
}
int count=0,i;

对于(i=1;i你可以这样做:

int char_count = 0;

char **p = array;

while (*p) {
    char_count += strlen(*p);
    ++p;
}
请注意,
strlen()
也将计算空格

如果不想计算空格或特殊字符,可以编写自己的长度函数,例如:

int string_length (const char *str) {
    int len = 0;

    while(*str) {
        /* Count only lower-case letters a-z. */
        if (*str >= 'a' && *str <= 'z') ++len;
        ++str;
    }

    return len;
}
int字符串长度(const char*str){
int len=0;
while(*str){
/*只计算小写字母a-z*/

如果(*str>='a'&&&*str您可以这样做:

int char_count = 0;

char **p = array;

while (*p) {
    char_count += strlen(*p);
    ++p;
}
请注意,
strlen()
也将计算空格

如果不想计算空格或特殊字符,可以编写自己的长度函数,例如:

int string_length (const char *str) {
    int len = 0;

    while(*str) {
        /* Count only lower-case letters a-z. */
        if (*str >= 'a' && *str <= 'z') ++len;
        ++str;
    }

    return len;
}
int字符串长度(const char*str){
int len=0;
while(*str){
/*只计算小写字母a-z*/

如果(*str>='a'&&*str也许您应该使用元素0到999,而不是1到1000。否则,元素0未定义,任何试图维护您的程序的人如果试图访问它,都会遇到意外错误。也许您应该使用元素0到999,而不是1到1000。否则,元素0未定义,任何人都尝试使用g要维护您的程序,如果他们试图访问它,将遇到意外错误。您只需要一个字符串,而不是1000。您将其长度添加到运行总数中。您只需要一个字符串,而不是1000。您将其长度添加到运行总数中。