2014-02-09 51 views
-5

当我尝试编译codeblocks给定的代码。它给了我的预期(在加密方法的签名文本之前编译错误谁能告诉我为什么会这样编译错误C

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


void encrypt(string text , int key) 
{ 

    for(int i = 0, n = strlen(text); i < n ; i ++) 
    { 
    if((text[i] >= 'A' && text[i] <='Z') || (text[i] >= 'a' && text[i] <='z')) 

      printf("%c", (text[i] + (key % 26))); 
    else 

      printf("%c",text[i]); 
} 

} 


int main() 
{ 
printf("Enter any String: \n"); 
string text; 
scanf("%s", &text); 
int x; 
printf("Enter Key: \n",&x); 
encrypt(text,x); 

return 0; 
} 
+7

'字符串不是C中的东西... –

+0

@OliCharlesworth我已经包含字符串库之前我的代码,那么为什么它给我一个错误? –

+1

因为'字符串'不是C中的东西。 http://en.cppreference.com/w/c/string/byte获得''标题的内容。 –

回答

1

使用:?

char text[100]; 
scanf("%s", text); 

100的最大长度字符串,所以用户必须在这种情况下最多插入99个字符(最后一个用于字符串终止符)

+4

“所以用户不能插入超过99个字符” - 他当然可以,然后它会失败,导致缓冲区溢出和未定义的行为。 'scanf()'不应该被使用。改用'fgets()'。 – 2014-02-09 11:40:27

+0

我明白了你的观点。谢谢 –

+0

@ H2CO3是的,你说得对。我重写了我的答案 – drolando