Password Validation | Sololearn: Learn to code for FREE!

+3

Password Validation

Hello everybody, I am working on password validation problem for a long time, I've almost done it totally, But I'm stuck, Here is my code https://code.sololearn.com/c8M95T82roIe/?ref=app I've checked my code many times ,But i just can't figure out the problems, Kindly help.

c++

5/1/2022 5:23:54 AM

Manav Roy

65 Answers

New Answer

+10

Line 10 if(isdigit(Password[index]!=0)) Should be if(isdigit(Password[index])!=0) Actually, should be if(isdigit(Password[index])) But I chose to stick with your style. And there is one case missing: What happens if the password does not have ANY special characters?

+4

The code I am looking at? Line 10 has not yet been fixed there. If you are using a new code then post theupdated version, please.

+4

You're welcome :) Glad that it is finally solved now 👍 Out of curiosity I was working on it meself in C. The result of which is in my code bits. If you are curious, feel free to check it out :)

+3

Ani Jona 🕊 Modified, Problem haven't solved yet though, My code is not working with the following case , Don't know why. PassWord19$#

+3

Yes, your algorithm is rather convoluted, I must say. However, which is the current version that is still not fixed?

+3

Bob_Li Jayakrishna🇮🇳 Ani Jona 🕊 Solved- https://code.sololearn.com/c8M95T82roIe/?ref=app Thank you guys :)

+2

Manav Roy What is your IsTwoNumbers() function do? What is the need of it? It always prints weak on a digit encounter at last digits....!

+2

Ani Jona 🕊 and Jayakrishna🇮🇳 In IsTwoNumbers function,I basically checked if the password contains two numbers or not. If so ,Then get out of function by return keyword for further processing in main function, But if it cannot find any digit throughout the loop iteration, The print Weak and exit(0); By the following code in main function,I checked if the password contains ANY digit or not ,If yes, Then check if it contains two digits by putting Password and i+1 in IsTwoNumbers function. for(int i=0;i<length;i++) { if(isdigit(Password[i])!=0) { IsTwoNumbers(Password,i+1); break; } if(i==length-1) { cout<<"Weak"; exit(0); } }

+2

It seems to be working now.

+2

I'll be unsubscribing since there is enough traffic on here. Please mention me if anyone wants to reach me :)

+2

#include<stdio.h> #include<string.h> int main(){ int i, n=0, m=0; char s[30]; scanf("%s",s); if(strlen(s) >= 7){ for(i=0; s[i]!='\0'; i++){ if(s[i]=='#'||s[i]=='$'||s[i]=='%'||s[i]=='&'||s[i]=='*'||s[i]=='!'||s[i]=='@') ++n; if(s[i]=='0'||s[i]=='1'||s[i]=='2'||s[i]=='3'||s[i]=='4'||s[i]=='5'||s[i]=='6'||s[i]=='7'||s[i]=='8'||s[i]=='9') ++m; } } if(n>=2 && m>=2) printf("Strong"); else printf("Weak"); return 0; }

+2

Vaibhav Singh That's the C version.

+2

Bob_Li i know the task. Also about special characters, am saying same. Seems you not read my all replys.. Read by date sorted way, for clarity.. And also expecting that input don't have special characters other than specified in description. But Manav Roy code is taking all other into count. May it cause problem I thought but now I guess it won't affect. I just letting @Manav go on his own approach...

+2

Bob_Li Yes about that also I mentioned more times.. His approach is isalpha(..)==0 && isdigit(..)== 0 means it's a special character... It works but he is having redudent checks..

+2

Bob_Li Yes. That's why I said before "Task description asked to check special characters [email protected]#$%&* but your also accouting other special character like +-_?.. So if input contains these characters then it may fail otherwise I think it works..." But I guess now, input don't have special characters other than mentioned in description I guess. So not efficient but this approach works.. I just letting Manav Roy go on his own approach firstfirst from start.

+2

Manav Roy congrats.. Finally.. Hope you try to simplify it.. You're welcome..

+2

Manav Roy Congratulations! You powered through it. In learning, the hard way is often more valuable than the easy way... and you took the hard path. Respect.

+2

Jayakrishna🇮🇳 Bob_Li Thanks ❤️

+1

Manav Roy as you saying .. In IsTwoNumbers function,I basically checked if the password contains **two numbers or not." where are you checking two numbers or not? I can't find it.. It doing like : If input : 1a2 On '1' it calls function but on 'a' it returns back and breaking loop.. Else for ex: a1234 On 1 it calls function and and comes out loop after 4 and prints weak and exits.. . Why you need functions here..? Instead why don't you use a counter and just increment it's value.. after last check according to description just...

+1

Oh.. Wait. You want both to return 0.. .. No. Privious one is better.. Reply code works for single character.. But you need for all characters to check.. But why you using functions here instead of just a counter required.. It is more confusing..