2014-01-05 61 views
-2

舉例來說,我的輸出是:thwhglHDGQYE836如何混合C中的輸出字符?

有沒有辦法讓它變成輸出?如果有辦法,一些建議會很好謝謝:)。

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

int main(void) { 
    int i; 
    char c; 

    srand(time(NULL)); 
    printf("Your Random Password is: "); 
    for (i = 0; i < 6; ++i) { 
     c = 'a' + rand() % ('z' - 'a' + 1); 
     printf("%c", c); 
    } 
    for (i = 0; i < 6; ++i) { 
     c = 'A' + rand() % ('Z' - 'A' + 1); 
     printf("%c", c); 
    } 
    for (i = 0; i < 3; ++i) { 
     c = '1' + rand() % ('1' - '9' + 1); 
     printf("%c", c); 
    } 

    printf("\n\n"); 
    system("pause"); 
} 
+0

你的意思你的'input'是'thwhglHDGQYE836'? –

+1

你是什麼意思的「混亂」?目前的產出有什麼問題? – interjay

+0

你的意思是你想要將輸出進行混洗,使其不是嚴格的小寫字母大寫順序?如果是這樣,那麼使用洗牌算法。 – tabstop

回答

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

int main(void) { 
    char password[6 + 6 + 3 + 1]; 
    int i, j=0, len=sizeof(password)-1; 

    srand(time(NULL)); 
    printf("Your Random Password is: "); 
    for (i = 0; i < 6; ++i) 
     password[j++] = 'a' + rand() % ('z' - 'a' + 1); 
    for (i = 0; i < 6; ++i) 
     password[j++] = 'A' + rand() % ('Z' - 'A' + 1); 
    for (i = 0; i < 3; ++i) 
     password[j++] = '1' + rand() % ('1' - '9' + 1); 
    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; 
     } 
    printf("%s\n\n", password); 
    system("pause"); 
} 
+0

比你這麼多! – DigitalD