C语言,如何在二维数组中找到单词的长度?

时间:2022-08-28 21:29:11

I am trying to make a condition for a for loop where i have to say (c<wordlength) but im not sure how to find the length of the word.

我正在尝试为for循环建立一个条件,在那里我必须说(c ),但是我不确定如何找到单词的长度。

so lets say i have an arrary called...

假设我有一个arrary。

char shopping[10][10]={"BAGELS","HAM","EGGS"};

what is the right syntax to find that shopping[0] has 6 letters?

什么是正确的语法发现购物[0]有6个字母?

4 个解决方案

#1


5  

The right syntax is

正确的语法是

strlen(shopping[0])

This returns a value of type size_t that does not include the NUL terminator.

这返回一个size_t类型的值,它不包含NUL终止符。

See man strlen for details.

详见man strlen。

If you are using strlen(unchanging_string) as the terminal condition of a loop, it is prudent to call it once before the loop instead of calling it on every iteration.

如果您使用strlen(unchanging_string)作为循环的终端条件,那么最好在循环之前调用它一次,而不是在每次迭代中调用它。

An alternative way to loop over the characters of shopping[0] is as follows:

循环浏览购物[0]字符的另一种方法如下:

char *s = shopping[0];
while (*s) {
  /* (*s) is the current character */
}

#2


5  

size_t len = strlen(shopping[0]);

Note, though, that you should not write:

但请注意,你不应该这样写:

for (size_t i = 0; i < strlen(shopping[0]); i++)

This can lead to bad performance on long strings. Use:

这会导致长字符串的性能下降。使用:

size_t len = strlen(shopping[0]);
for (size_t i = 0; i < len; i++)

#3


2  

Actually shopping[0] has 7 chars one you forgot \0 char that is for string termination. Although strlen() give you length of string, that number of char before \0.

实际上,购物[0]有7个chars 1你忘记了\0 char表示字符串终止。虽然strlen()给出字符串的长度,但是在\0之前的字符数。

strlen(shopping[0])

But Total char are strlen(shopping[0]) + 1 = 7

但是Total char是strlen(购物[0])+ 1 = 7

In memory your shopping[0] is something like:

在记忆中,你的购物[0]是这样的:

+----+----+----+---+---+----+----+----+---+---+ 
| 'B'| 'A' |'G'|'E'|'L'| 'S'|'\0'| 0  | 0 |   |
+----+----+----+---+---+----+----+----+---+---+
                              ^ `\0` also a char in shopping[0]

Edit:

编辑:

As I read your question again you says 6 letters So strlen(shopping[0] ) is your answer give you 6.

当我再次读你的问题时你说6个字母所以strlen(购物[0])是你的答案给你6个字母。

Because you wants a loop to find number of letters (char in my answer non '\0') then calling strlen() is useless. I would like that you should take benefit of null termination string in C:

因为您希望循环查找字母的数量(在我的答案中是char而不是'\0'),所以调用strlen()是没有用的。我希望你能利用C中的空终止字符串:

int num=0;
for(num = 0; shopping[0][num]!='\0'; num++);
printf("\n  number of letters are %d\n",num);

I think other answers are not good, they are using strlen() unnecessary. If I am missing something, Please let me know

我认为其他答案不太好,它们使用strlen()没有必要。如果我漏掉了什么,请告诉我

#4


1  

You can write the code using a for loop to find the size of each element in the shopping array:

您可以使用for循环编写代码来查找购物数组中每个元素的大小:

for(i =0;i<10;i++)
{
    j=0;
    while(shopping[i][j])
    {
        j++;
    }
}

where j will return the size of each shopping array element.

其中j将返回每个购物数组元素的大小。

#1


5  

The right syntax is

正确的语法是

strlen(shopping[0])

This returns a value of type size_t that does not include the NUL terminator.

这返回一个size_t类型的值,它不包含NUL终止符。

See man strlen for details.

详见man strlen。

If you are using strlen(unchanging_string) as the terminal condition of a loop, it is prudent to call it once before the loop instead of calling it on every iteration.

如果您使用strlen(unchanging_string)作为循环的终端条件,那么最好在循环之前调用它一次,而不是在每次迭代中调用它。

An alternative way to loop over the characters of shopping[0] is as follows:

循环浏览购物[0]字符的另一种方法如下:

char *s = shopping[0];
while (*s) {
  /* (*s) is the current character */
}

#2


5  

size_t len = strlen(shopping[0]);

Note, though, that you should not write:

但请注意,你不应该这样写:

for (size_t i = 0; i < strlen(shopping[0]); i++)

This can lead to bad performance on long strings. Use:

这会导致长字符串的性能下降。使用:

size_t len = strlen(shopping[0]);
for (size_t i = 0; i < len; i++)

#3


2  

Actually shopping[0] has 7 chars one you forgot \0 char that is for string termination. Although strlen() give you length of string, that number of char before \0.

实际上,购物[0]有7个chars 1你忘记了\0 char表示字符串终止。虽然strlen()给出字符串的长度,但是在\0之前的字符数。

strlen(shopping[0])

But Total char are strlen(shopping[0]) + 1 = 7

但是Total char是strlen(购物[0])+ 1 = 7

In memory your shopping[0] is something like:

在记忆中,你的购物[0]是这样的:

+----+----+----+---+---+----+----+----+---+---+ 
| 'B'| 'A' |'G'|'E'|'L'| 'S'|'\0'| 0  | 0 |   |
+----+----+----+---+---+----+----+----+---+---+
                              ^ `\0` also a char in shopping[0]

Edit:

编辑:

As I read your question again you says 6 letters So strlen(shopping[0] ) is your answer give you 6.

当我再次读你的问题时你说6个字母所以strlen(购物[0])是你的答案给你6个字母。

Because you wants a loop to find number of letters (char in my answer non '\0') then calling strlen() is useless. I would like that you should take benefit of null termination string in C:

因为您希望循环查找字母的数量(在我的答案中是char而不是'\0'),所以调用strlen()是没有用的。我希望你能利用C中的空终止字符串:

int num=0;
for(num = 0; shopping[0][num]!='\0'; num++);
printf("\n  number of letters are %d\n",num);

I think other answers are not good, they are using strlen() unnecessary. If I am missing something, Please let me know

我认为其他答案不太好,它们使用strlen()没有必要。如果我漏掉了什么,请告诉我

#4


1  

You can write the code using a for loop to find the size of each element in the shopping array:

您可以使用for循环编写代码来查找购物数组中每个元素的大小:

for(i =0;i<10;i++)
{
    j=0;
    while(shopping[i][j])
    {
        j++;
    }
}

where j will return the size of each shopping array element.

其中j将返回每个购物数组元素的大小。