2014-02-09 178 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