简体   繁体   English

如何确定字符串的长度(不使用strlen())

[英]How to determine the length of a string (without using strlen())

size_t stringlength(const char *s)

Using this function, how could find the length of a string? 使用此函数,如何找到字符串的长度? I am not referring to using strlen() , but creating it. 我不是指使用 strlen() ,而是创建它。 Any help is greatly appreciated. 任何帮助是极大的赞赏。

Cycle/iterate through the string, keeping a count. 循环/遍历字符串,保持计数。 When you hit \\0 , you have reached the end of your string. 当你点击\\0 ,你已到达字符串的末尾。

The basic concepts involved are a loop, a conditional (to test for the end of string), maintaining a counter and accessing elements in a sequence of characters. 涉及的基本概念是循环,条件(测试字符串的结尾),维护计数器和访问字符序列中的元素。

Note : There are more idiomatic/clever solution. 注意 :有更多惯用/聪明的解决方案。 However OP is clearly new to C and programming (no offense intended, we all started out as newbies), so to inflict pointer arithmetic on them as one of solutions did or write perhaps overly terse/compact solutions is less about OP's needs and more about a demonstration of the posters' programming skills :) Intentionally providing suggestions for a simple-to-understand solution earned me at least one downvote (yes, this for " imaginary code " that I didn't even provide. I didn't want to ready-serve a code solution, but let OP figure it out with some guidance). 然而,对于C和编程而言,OP显然是新的(没有违法行为,我们都是初学者),因此,作为解决方案之一或写作可能会对它们施加指针算法 ,或许过于简洁/紧凑的解决方案不是关于OP的需求而是更多关于海报编程技巧的演示:)故意为简单易懂的解决方案提供建议至少为我赢得了一个downvote(是的,这是我想不到的“ 虚构代码 ”。我不想准备好一个代码解决方案,但让OP在一些指导下弄清楚它。

Main Point : I think answers should always be adjusted to the level the questioner. 要点 :我认为应该始终将答案调整到提问者的水平。

size_t stringlength(const char *s) {
   size_t count = 0;
   while (*(s++) != '\0') count++;
   return count;
}

The confusing part could be the expression *(s++) , here you're moving the pointer to point the next character in the buffer using the ++ operator, then you're using the dereferencing operator * to get the content at the pointer position. 令人困惑的部分可能是表达式*(s++) ,这里你移动指针使用++运算符指向缓冲区中的下一个字符,然后你使用解除引用运算符*来获取指针位置的内容。 Another more legible approach would be: 另一个更清晰的方法是:

size_t stringlength(const char *s) {
   size_t count = 0;
   while (s[count] != '\0') count++;
   return count;
}

Another couple of reference versions (but less legible) are: 另外几个参考版本(但不太清晰)是:

size_t stringlength(const char *s) {
   size_t count = 0;
   while (*s++) count++;
   return count;
}


size_t stringlength(const char *s) {
   const char* c = s;
   for (; *c; c++);
   return c - s;
}

Although the code stated here is just a reference to give you ideas of how to implement the algorithm described in the above answer, there exists more efficient ways of doing the same requirement (check the glibc implementation for example, that checks 4 bytes at a time) 虽然这里所说的代码只是一个参考,为您提供如何实现上述答案中描述的算法的想法,但存在更有效的方法来执行相同的要求(例如,检查glibc实现 ,一次检查4个字节)

This might not be a relevant code, But I think it worth to know. 这可能不是相关的代码,但我认为值得知道。 Since it saves time... 因为它节省了时间......

int a[] = {1,2,3,4,5,6};

unsigned int i,j;

i = &a; //returns first address of the array say 100

j = &a+1; //returns last address of the array say 124

int size = (j-i)/sizeof(int); // (j-i) would be 24 and (24/4) would be 6

//assuming integer is of 4 bytes

printf("Size of int array a is :%d\n",size);

And for strings :: 而对于字符串::

char a[] = "Hello";

unsigned int i,j;

j = &a+1; //returns last address of the array say 106

i = &a; //returns first address of the array say 100


printf("size of string a is : %d\n",(j-i)-1); // (j-i) would be 6

If you are confused how come &a+1 returns the last address of the array, check this link. 如果你感到困惑怎么来&a + 1返回数组的最后一个地址,请检查此链接。


Assuming s is a non-null pointer, the following function traverses s from its beginning until the terminating zero is found. 假设s是非空指针,则以下函数从其开始遍历s,直到找到终止零。 For each character passed s++; 对于传递s++;每个字符s++; count is incremented count++; count是递增count++; .

size_t stringlength(const char *s) {
   size_t count = 0;
   while (*s) {
     s++;
    count++;
   }
   return count;
}

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

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