2016-05-08 6 views
0

私が作成しようとしているこのプログラムをさらに進める方法はわかりません。Cのパスワード入力の検証

パスワードに、大文字、小文字、数字のいずれかが1つ以上あるかどうかを確認するためのパスワード入力を検証します。

現時点では一部が壊れています。たとえば、偽の真の文。 main関数の "undynamic" char配列。私は現時点でそれを作る方法も知らない。しかし、それは私が探しているものを説明します。

あまりにも多くのコードを書くことなくこれを検証するにはどうすればよいですか?

これは私の現在のコードです:stringの

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

int passval(char pw[]) 
{ 
    int x; 

    for (x = 0; x < sizeof(pw); x++) { 
     if ((isalnum(pw[x])) || (ispunct(pw[x]))) { 
      return 0; 
     } else { 
      return 1; 
     } 
    } 

    return 0; 
} 

int main() 
{ 
    char password[20]; 

    printf("Enter password: "); 
    scanf("%s", password); 

    if (passval(password) == TRUE) { 
     printf("Password is TRUE"); 
    } 

    return 0; 
} 
+4

ファーストのような何かをするだろう、 'はsizeof(PW)は、'文字列の長さを取得する方法はありません。 ''\ 0''で停止するか、 '' strlen() ''を使って文字列の長さを取得してください。ループ状態で 'strlen()'を使うと、各繰り返しで呼び出される可能性があるため、パフォーマンスが悪くなることに注意してください。 – MikeCAT

+0

あなたの機能からすぐに戻ります。あなたはフラグを使用する必要があります –

+1

[sizeof(param \ _array)がポインタのサイズなのはなぜですか?](http://stackoverflow.com/questions/11622146/why-sizeofparam-array-is-the-size-of -pointer) –

答えて

0
  1. スキャン。
  2. 見つかる文字が存在する場合は、フラグを立てます。
  3. 必要なフラグがすべて生成されている場合は「有効」と表示されます。

実装例:

#include <ctype.h> 

int passval(const char pw[]) 
{ 
    size_t x; 
    unsigned char c; /* making this unsigned is important: 
     char may be negative and passing it to isupper(), etc. may invoke undefined behavior */ 
    int upperExists = 0, lowerExists = 0, numberExists = 0; 

    for (x = 0; pw[x] != '\0'; x++) { 
     /* fetch the character */ 
     c = pw[x]; 
     /* raise flags when the character is required kind */ 
     upperExists = upperExists || isupper(c); 
     lowerExists = lowerExists || islower(c); 
     numberExists = numberExists || isdigit(c); 
    } 

    /* check if all of required flags are raised */ 
    return upperExists && lowerExists && numberExists; 
} 
+0

良いですが、すべての条件が真であるとみなされてもNULLになるまで文字列全体を不必要にループします – 4pie0

-1

ええ、あなたがフラグを使用する必要があります。 int型FLAG1 = 0

1
#include <stdio.h> 
#include <string.h> 
#include <ctype.h> 

int 
password_validate(const char *pass) 
{ 
    int upper = 0, lower = 0, digit = 0; 
    if (pass == NULL || strlen(pass) == 0) 
     return -1; 

    do 
    { 
     if (isupper(*pass)) 
      upper = 1; 
     if (islower(*pass)) 
      lower = 1; 
     if (isdigit(*pass)) 
      digit = 1; 
    } while ((!lower || !upper || !digit) && *(++pass)); 

    return (*pass != '\0' ? 0 : (upper == 0 ? -2 : (lower == 0 ? -3 : -4))); 
} 

のように(追加の例を強調するためにあなたのアレックス・ポーグに感謝)コーナーケースのいくつかを理解することを確認するコードサンプルに以下のリンクを参照してください、どのようにこの機能はそれらを処理します。

https://ideone.com/GiOGkj

+1

良いです!私のdownvoteをupvoteに変更しました:) – AlexPogue

0

私のソリューションは\0文字で終わる文字列を解析に基づいて、少なくとも一つの資本のchar、小さな文字、そしてどのようにORゲート機能などの数字をチェック..

あなたが関数内ではsizeof()を行うことはできませんので、ポインタに関数減衰に渡されTry Online

int passval(char * p) 
{ 
    int capital=0, small=0, digit=0; 

    while (*p && !(capital && small && digit)) 
     capital = (*p>='A' && *p<='Z' ? 1 : capital), 
     small = (*p>='a' && *p<='z' ? 1 : small ), 
     digit = (*p>='0' && *p<='9' ? 1 : digit ), 
     p++ ; 

    return capital && small && digit; 
} 
+1

意味が些細なものではないマジックナンバーを使うと、この解決はASCII文字コードなどに依存します。 – MikeCAT

+0

@MikeCAT私は同意します、今すぐ修正しました –

+1

whileループの終了条件なので、return文で '!(* p)'はすでに保証されていませんか? – AlexPogue

0

配列は、代わりに文字列の長さを渡すstrlen()

私はすべてのこの

#include <ctype.h> 
#include <stdbool.h> 

bool validatePassword(const char* pw, const int len) 
{ 
    int x; 
    bool upperCase = false; 
    bool lowerCase = false; 
    bool number = false; 

    for (x = 0; x < len; x++) 
    { 
     if (pw[x] == toupper(pw[x])) 
     { 
     upperCase = true; 
     } 
     else if (pw[x] == tolower(pw[x])) 
     { 
     lowerCase = true; 
     } 
     else if (isdigit(pw[x])) 
     { 
     number = true; 
     } 
    } 

    return upperCase && lowerCase && number; 
}