繁体   English   中英

为什么strlen函数在这种for循环条件下不起作用?

[英]Why does strlen function not work in this for loop condition?

我正在学习C并且有疑问。 在本练习中,我必须编写一个名为double dutch的游戏,在该游戏中您将学习如何练习弦乐。 我遇到的问题是,由于for循环条件(在第一个for循环中),程序停止执行。 当我打印字符串的长度时,strlen()函数首先在main和ayInFrontOfConsonant函数中运行良好,但我不知道为什么程序会停止工作。 在Xcode中,我收到消息:线程1:EXC_BAD_ACCESS。 很感谢任何形式的帮助。

void ayInFrontOfConsonant(char *str) 
{
    char consonants[42] = { 'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 
'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z', 'B', 
'C', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 
'T', 'V', 'W', 'X', 'Y', 'Z'};


    int a=(int)strlen(str);

    printf("\n Length of str in ay function:   %d\n",a);

    int i=0,j=0;
    for(i=0;i<strlen(str);i++)      //problem is here
    {
        for(j=0;j<strlen(consonants);j++)
        {
            if(str[i]==consonants[j])
            {
                //insertChar(str, 'a', i);

            }
        }
    }
}

int main()
{
    int a=0;
    printf("** Welcome to the Double Dutch game **\n");
    char myString[36];
    printf("Please enter a string: ");
    scanf("%[^\n]s", myString);

    a=strlen(myString);
    printf("Length of string in main: %d\n",a);

    ayInFrontOfConsonant(myString);


    printf("Double dutch traslation: %s\n",myString);


    return 0;

}

您的数组没有null终止符。

取而代之的是,使用sizeof consonants / sizeof *consonants -或在此特定情况下,因为sizeof *consonants肯定为1 ,则仅使用sizeof consonants

您不应该在for循环的条件下使用strlen() ,因为它每次都会遍历字符串,直到找到您所缺少的null终止符为止

char consonants[42] = { 'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 
    'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z', 'B', 
    'C', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 
    'T', 'V', 'W', 'X', 'Y', 'Z'};

如果您使用

const char *consonant = "bcdf ...";

相反,编译器将添加'\\0'终止符,您也可以显式添加它

char consonants[] = { 'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 
        'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z', 'B', 
        'C', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 
        'T', 'V', 'W', 'X', 'Y', 'Z', '\0'};

程序员可能会改写这个,

#include <stdlib.h>

void ayInFrontOfConsonant(char *str) 
{
    char consonants[] = {
        'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 
        'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z', 'B', 
        'C', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 
        'Q', 'R', 'S', 'T', 'V', 'W', 'X', 'Y', 'Z'
    };

    for (size_t i = 0; str[i] != '\0'; i++) {
        for (size_t j = 0; j < sizeof consonants; ++j) {
            if (str[i] == consonants[j]) {
                // Do here whatever you wanted to do
            }
        }
    }
}

但这不是真的,因为不需要扫描整个辅音数组,因为可以对它们进行排序,并且可以使用二进制搜索来改善算法。

当您编写类似char consonants [42] = { ... }的语句时,会发生以下三种情况之一:

如果您有43个或更多字符,编译器会给您一个错误。

如果您有41个或更少的字符,则编译器将用零填充数组的其余部分,并且strlen()将起作用,因为字符后面有一个nul字节。

如果您恰好有42个字符,则编译器将数组完全填充到末尾。 没有尾随零字节。 strlen将不起作用。

实际上,您没有理由要数字符。

char consonants [] = "bcdfgh..." 

会完全按照您想要的去做。

暂无
暂无

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

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