Originally Posted by
psybuck2002us
Hey, i tried running this in Borland 5.02 and it did not run properly. Please reply and tell me which compiler you used. I even put in #include <conio.h> and added the getch(); right before the last brace and it still didn't do what it is supposed to do. I read through your code though and for the most part it seems that you knew the trick to solving the problem(Using the ASCI values of the letters of the password).
For that i give you a thumbs up, but try running over your code and see if you got the same output when you enter the password "apple" as i had posted. Also, i did not specify that the password must be five letters MAX, so there is where a bit of challenge comes in.(Hint: To make things easier, try using the getc(stdin); function within the body of your loop to perform the necessary conversions one character at a time. The getc(stdin); function also returns an integer value because it converts the inputed character into an integer). Good Luck bredren.
I am posting my solution nextweek, just studying for my exams now.
I used MSVC++ 6.0. I got the same output you did with apple. I only used 5 letters because the sample input you provided was 5 characters long. The code can be adjusted quite easily. Here's the code adjusted for passwords up to 99 characters.
Code:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define MAX 100
void main(){
unsigned int x=0;
int i=0,loop=0;
char pwd[MAX];
do{
printf("Enter your password:");
gets(pwd);
for(x=0;x<strlen(pwd);x++){
if( (pwd[x] > 96 && pwd[x] < 123) || (pwd[x] > 64 && pwd[x]< 91)){
loop=1;
pwd[x] = tolower(pwd[x]); //converts letter to lowercase
if(pwd[i]==122) //if the char is z
pwd[i]=97; //char is a
else pwd[i]+=1; //else increment by one
if((pwd[i]-5) < 97) //if char is less than 5 chars away from a
pwd[i] = 122 - ((pwd[i]+1)-96); //calculate new value
else pwd[i]-=5; //else decrement by 5
if((pwd[i]+10) > 122) //if char is less than 10 chars away from z
pwd[i] = 97 + (10-(122 - (pwd[i]-1))); //calculate new value
else pwd[i]+=10; //else increment by 10
i++;
}
else{
printf("Invalid character entered\n");
loop=0;
break;
}
}
}while(loop==0);
}
I wouldn't use the getc() function because the for loop already traverses the array one char at a time.
Last edited by nigelt; Apr 23, 2005 at 04:52 PM.
Nexus S - Android 2.3.3 CM7, Jame Bond kernel with BLN, ext4 & full voodoo
Bold 9700 - OS6.0.0.448