GeeksforGeeks » C/C++ Programming Questions
nagarro coding question
(3 posts)-
We need to write the function to check the password entered is correct or not based on the following conditions..
a) It must have atleast one lower case character and one digit.
b)It must not have any Upper case characters and any special characters
c) length should be b/w 5-12.
d) It should not have any same immediate patterns like
abcanan1 : not acceptable coz of an an pattern
abc11se: not acceptable, coz of pattern 11
123sd123 : acceptable, as not immediate pattern
adfasdsdf : not acceptable, as no digits
Aasdfasd12: not acceptable, as have uppercase character -
void swap5(char** str, int x, int y){
char tmp;
tmp = (*str)[x];
(*str)[x] = (*str)[y];
(*str)[y] = tmp;
}void swapreverse(char** str, int len, int x, int y){
if (len <= 0)
return;if (x == y && len % 2 == 1){
return;
}
if (((y - x) == 1) && len % 2 == 1){
return;
}swapreverse(str, len - 2, x + 1, y - 1);
swap5(str,x,y);
}bool Immediate(string original){
bool retval(false);
string tmpduplicate;
int length1(0);
int length2(0);if (original.length() == 0)
return false;char *ztest = new char[original.length() + 1];
strcpy(ztest,original.c_str());
length1 = original.length() / 2;
length2 = original.length() - length1;swapreverse(&ztest, strlen(ztest),0, strlen(ztest) - 1);
swapreverse(&ztest, length1,0, length1 - 1);
swapreverse(&ztest, length2,length1 ,strlen(ztest) - 1 );
cout << original << " " << ztest << endl;
if (original.length() >= 2 && strcmp(ztest, original.c_str()) == 0){
delete [] ztest;
return true;
}
else{
delete [] ztest;
}
for (int mylen = original.length() - 1; mylen > 1; mylen--){
retval = Immediate(original.substr(1,mylen));
if (retval)
return true;
}
return retval;
} -
Here is the pseudo code for the proposed solution. The function Immediate(..) is defined in the previous post.
bool PasswordCheck(string potential){
if any char UpperCase return false;
if any char special char return false;
if (numdigits == 0) return false;
if (numlowercasechars = 0) return false;
if length < 5 || or length > 12) return false;
if (Immediate(potential) == false;
return true;
}bool Immediate(string original){
bool retval(false);
string tmpduplicate;
int length1(0);
int length2(0);if (original.length() == 0)
return false;char *ztest = new char[original.length() + 1];
strcpy(ztest,original.c_str());
length1 = original.length() / 2;
length2 = original.length() - length1;swapreverse(&ztest, strlen(ztest),0, strlen(ztest) - 1);
swapreverse(&ztest, length1,0, length1 - 1);
swapreverse(&ztest, length2,length1 ,strlen(ztest) - 1 );
cout << original << " " << ztest << endl;
if (original.length() >= 2 && strcmp(ztest, original.c_str()) == 0){
delete [] ztest;
return true;
}
else{
delete [] ztest;
}
for (int mylen = original.length() - 1; mylen > 1; mylen--){
retval = Immediate(original.substr(1,mylen));
if (retval)
return true;
}
return retval;
}
Reply
You must log in to post.