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*
,是指向相应字符串文字的第一个字符的指针。Yeschar*数组[]={“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)));
}