简体   繁体   English

在不使用 string.h 库的情况下计算 C 中字符串的长度(使用 char 指针)

[英]Count length of string in C without using string.h library (using char pointers)

I need to count the length of a string in C without using the string.h library, using char pointers.我需要使用 char 指针在不使用 string.h 库的情况下计算 C 中字符串的长度。 So far all the methods I've seen use the string.h library.到目前为止,我看到的所有方法都使用 string.h 库。 Is it possible to do it without i?没有我可以做到吗?

Yes it is possible, the end of a string in C is usually null terminated, so you can loop through the string until you find the '\0' character, counting each as you go.是的,有可能,C 中字符串的结尾通常是 null 终止,因此您可以循环遍历字符串,直到找到“\0”字符,将每个字符计数为 Z34D1F91FB2E514B8576FAB1A75A8。

When talking about strings in C, you're essentially talking about an array of char , terminated by the nil character ( '\0' ).在谈论 C 中的字符串时,您实际上是在谈论一个char数组,由 nil 字符 ( '\0' ) 终止。 The length of a string, therefore is:因此,字符串的长度为:

size_t len(const char *s) {
    // if no string was passed (null pointer), just return
    if (s == NULL)
        return 0;

    size_t ln;
    // iterate over string until you encounter the terminating char
    for (ln=0;s[ln] != '\0'; ln++);

    return ln;
}

As pointed out by @chux-ReinstateMonica: the correct type to return here is size_t , if you want to avoid potential overflow issues + you're looking for a drop-in replacement for the strlen() function in strings.h .正如@chux-ReinstateMonica 所指出的:这里返回的正确类型是size_t ,如果你想避免潜在的溢出问题+你正在寻找strlen() function in strings.h的替代品。 The length of a string can, after all, never be negative, so using the unsigned size_t makes more sense than the signed int .毕竟,字符串的长度永远不会是负数,因此使用 unsigned size_t比使用有符号int更有意义。

Now this obviously assumes the input was a valid string.现在这显然假设输入是一个有效的字符串。 If I were to do something like:如果我要做类似的事情:

int main( void ) {
    char * random = malloc(1024); // allocate 1k chars, without initialising memory
    printf("length is: %zu\n", len(random)); // use %zu because size_t is unsigned!
    return 0;
}

There's no guarantee the memory I allocated will even contain a terminating character, and therefore it's not impossible for the function to return a value greater than 1024. Same goes for something like:不能保证我分配的 memory 甚至会包含终止字符,因此 function 返回大于 1024 的值并非不可能。类似的情况也是如此:

char foo[3] = {'f', 'o', 'o'}; // full array, no terminating char

This string, in memory, could look something like this:这个字符串,在 memory 中,可能看起来像这样:

//the string - random memory after the array
| f | o | o | g | a | r | b | a | g | e | U | B | \0 |

Making it look like a string of 12 characters.使它看起来像一个 12 个字符的字符串。 If you then use the assumed length of 12 to write past the actual array, all bets are off (undefined behaviour).如果您随后使用假定的 12 长度来写入实际数组,则所有赌注都将关闭(未定义的行为)。

This string should've been:这个字符串应该是:

char foo[4] = {'f', 'o', 'o', '\0'}; // or char[4] foo = "foo";

You can read an old answer I gave elsewhere for some more details on how to get the length of a string passed to a function here您可以阅读我在其他地方给出的旧答案,以获取有关如何获取传递给 function 字符串长度的更多详细信息。

Here example:这里的例子:

size_t pos(char* s)
{
    size_t lenS; //len of string

    for (lenS = 0; s[lenS]; lenS++);

    return lenS;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM