简体   繁体   English

不使用strlen()计算并打印字符串的长度

[英]Compute and print the length of string without using strlen()

In this program, I am trying to compute and print the length of string without using strlen(). 在此程序中,我尝试不使用strlen()来计算和打印字符串的长度。 Why it always prints "length is : 0" 为什么总是打印“ length is:0”

#include <stdio.h>

int length(char str[])
{
    int a_string;
    for (a_string=0;str[a_string]!='\0';++a_string)
    {
        return a_string;
    }
}

int main()
{
    char line[80];
    printf("Enter string:\n");
    fgets(line,sizeof(line), stdin);
    printf("Length (including newline) is: %d\n",length(line));
    return 0;
}

Your return statement is inside the loop, so it exits right away on first iteration. 您的return语句在循环内,因此它在第一次迭代时立即退出。 Move it after the loop. 循环后将其移动。

In your for loop, you immediately return a_string which will have a value of 0 the first time. 在您的for循环中,您立即返回a_string ,它将在第一次时值为0 Instead, you should return a_string after the loop completes. 相反,您应在循环完成后返回a_string

Take the return outside of the for loop. 将返回返回到for循环之外。 As your code is now, the return is used every iteration of the loop. 就像现在的代码一样,循环的每次迭代都使用返回值。 and this is not what you want. 这不是你想要的。 change your code to: 将您的代码更改为:

int length(char str[])
{
    int a_string;
    for (a_string=0;str[a_string]!='\0';++a_string)
    {
    }
        return a_string;
}

Now the loop would run and a_string would be equal to the size of the string, and the return would be called only after the loop finishes its run, and not after the first iteration. 现在循环将运行,并且a_string等于字符串的大小,并且只有在循环完成运行之后才调用返回,而不是在第一次迭代后调用返回。

not the prettiest variant, but the fastest. 不是最漂亮的变体,而是最快的变体。 however, always use the standard function, it is often written in assembly language for a particular architecture, and most fastest, that you could write on the C 但是,请始终使用标准功能,对于特定体系结构,通常是用汇编语言编写的,并且最快可以在C语言上编写

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

size_t my_strlen(char *a)
{
        char *b;
        for (b = a; *a; a++);
        return a - b;
}

int main(void)
{
        char *str = "Hello world";
        printf("str len %d\n", my_strlen(str));
        return 0;
}

in your code your need change 在您的代码中您需要更改

for (a_string=0;str[a_string]!='\0';++a_string)
{
    return a_string;
}

to

    for (a_string=0;str[a_string]!='\0';++a_string)
    {
        ;
    }
return a_string

Another way, without strlen() and without increasing a counter: 没有strlen()且不增加计数器的另一种方式:

int length(char *str)
{
  const char *w = str;

  while(*str)
    str++;

  return str - w;
}
the code should not return until AFTER the calculation is complete.
currently the code returns before ever doing any counting
suggest the following, which you might evaluate to assure the 
returned value does not include the null byte

int length(char* str)
{
    int a_string;
    for (a_string=0; str[a_string]!='\0'; ++a_string);

    return a_string;
}

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

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