2014-01-07 71 views
0

所以我的代碼,當你輸入1時,輸出一個12個字符的字符串。 我希望能夠搜索字符串的一個字...搜索一個字的輸出字符串

我有代碼完成...但我不知道它適合我的代碼。 搜索一個詞的代碼是在底部,我不知道如何讓它搜索 字符串雖然。

任何人都可以幫忙嗎?

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


FILE*fp; 

char *symbols = "#~!$%^&*()+=<>?/@"; // Random Symbol is generated form this list 

int main(void) { 

char password[4 + 4 + 2 + 2 + 1]; // 4 Upper, 4 Lower, 2 Num, 2 Symbols 
int i, j=0, len=sizeof(password)-1; 
int menuNum = 0; 
char passwordCheck[15+1]; 

fp = fopen("passwords.txt", "a+"); //Opens the text file to save the Passwords 

srand(time(NULL)); 
printf("  Main Menu\n"); 
printf("********************************\n"); 
printf("\nEnter 1 to Generate a New Password: "); 
printf("\n\n"); 
printf("Enter 2 to Check Old Passwords: "); 
printf("\n\n"); 
printf("Enter 3 to Check Password for Small Words: "); 
printf("\n\n"); 

scanf("%d", &menuNum); // reads number 

if (menuNum == 1) // If 1 is entered on the Menu... 
{ 
    printf("********************************\n"); 
    printf("\nYour New Password is: \n\n"); 

// Each Password will Have 12 Characters(4 Uppercase letters, 4 Lowercase letters, 2 Numbers & 2 Symbols) 

for (i = 0; i < 4; ++i) 
    password[j++] = 'a' + rand() % ('z' - 'a' + 1); // Generates 4 random Lowercase characters 

for (i = 0; i < 4; ++i) 
    password[j++] = 'A' + rand() % ('Z' - 'A' + 1); // Generates 4 random Uppercase characters 

for (i = 0; i < 2; ++i) 
    password[j++] = '0' + rand() % ('0' - '9' + 1); // Generates 2 random numbers 

for (i = 0; i < 2; ++i) 
    password[j++] = symbols[rand() % strlen(symbols)]; // Generates 2 random symbols 

password[j] = '\0'; 
for(i = 0; i < sizeof(password)-1; ++i) 
    { 
    char c = password[i]; 
    j = rand() % len; 
    password[i] = password[j]; 
    password[j] = c; 
    } // Shuffles passwords Characters 

printf("%s\n\n", password); 
printf("********************************\n"); 
fprintf(fp, "\n%s", password); // Outputs the Generated Passoword to the text file 
fclose(fp); // Closes the text file 
system("pause"); 
} 


else if (menuNum == 2) 
{   
    printf("\nEnter your password for checking: "); 
    printf("%s", passwordCheck); // reads password 
    if (strlen(passwordCheck) > 15) // Checks length of Password 
    { 
     printf("'%s' is too long. Needs to be less then 15 Characters\n", passwordCheck); 
     system("pause"); 
    } 
    else if (strlen(passwordCheck) < 9) //Checks length of Password 
    { 
     printf("'%s' is too short. Needs to be more then 9 Characters\n", passwordCheck); 
     system("pause"); 
    } 
} 


else if(menuNum == 3) 
{ 
    if (strstr (passwordCheck, "car") != NULL); 
    { 
     printf("Password contains a common word, such as \"car\"\n\n"); 
     system("pause"); 
    } 
} 

}

+0

對不起...所以..輸出,產生一個12個字符的字符串...當我輸入1.當我輸入3時,我希望能夠搜索該字符串的一個小字,在這裏,我用「汽車」。我有爲這部分編寫的代碼...但我不知道如何讓它搜索字符串 – DigitalD

回答

0

,而不是其他,如果階梯。使用switch語句。

連代碼,其中menuNum等於2具有邏輯錯誤

else if (menuNum == 2) 
{   
printf("\nEnter your password for checking: "); 
printf("%s", passwordCheck); // just writes the string present in passwordCheck 
} 

此打印passwordCheck到屏幕沒有看過。

使用infinte while循環的所有三種情況下,因爲只有一個測試用例在給定的執行在passwordCheck運行和u嘗試檢查「汽車」,這將是要麼垃圾或無法訪問

//另一種選擇是嘗試從文件讀取密碼到密碼檢查變量,以便你可以檢查。

if (strstr (passwordCheck, "car") != NULL); // either un reachable or garbage value 
{ 
    printf("Password contains a common word, such as \"car\"\n\n"); 
    system("pause"); 
}