2017-08-25 107 views
-1

我在学C,我有问题。在这个练习中,我必须编写一个叫做双荷兰语的游戏,在那里你学会用字符串练习。我遇到的问题是,由于for循环条件(在第一个循环中)程序停止执行。当我打印字符串的长度时,strlen()函数在main和ayInFrontOfConsonant函数中工作良好,但我不明白程序停止工作的原因。在Xcode中,我得到以下消息:线程1:EXC_BAD_ACCESS。很感谢任何形式的帮助。为什么strlen函数在这个循环条件下不起作用?

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; 

}

+0

你的意见是什么? –

+0

我使用的输入是:“我喜欢写C代码” –

+0

如果你知道'strlen()'做了什么,那么你就不会那样使用它。 –

回答

3

你的数组有没有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 
      } 
     } 
    } 
} 

但不是真的,因为不需要扫描辅音全阵列,因为它们可以进行排序,你可以使用二进制搜索这会改善算法很多。

+0

请注意,'sizeof(constonants)/ sizeof(* constonant)'技术只适用于constonants是数组的情况。将它作为参数传递给一个函数,它将被转换为一个指针,并且该技术将不起作用(因此需要以不同的方式传递长度)。 – Peter

2

当你写如下语句char consonants [42] = { ... },三种情况之一发生:

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

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

如果您只有42个字符,编译器会将数组完全填充到最后。没有结尾的零字节。 strlen将不起作用。

在现实中,你没有理由数字。

char consonants [] = "bcdfgh..." 

会做你想要的。

+0

“*会做你想要的东西*”,更好的解决方案实际上是保持数组,但使用'sizeof'而不是'strlen'。 – ikegami

相关问题