Reversing Password

Discussion in 'Software' started by someguy85, Dec 2, 2009.

  1. someguy85

    someguy85 Private E-2

    Hi again everyone. I have an assignment that involves verifying a user specified password. I have everything working great except for one thing.....I can't figure out how to reverse the password by using the 'at' and 'append' functions. We're not allowed to simply print the password backwards. Any help on this would be great. Also, if there are any alternative ways to reverse the code, I would be interested in seeing them. Isn't there a library function that reverses strings? My code so far is below.

    Code:
    
    #include <cctype>
    #include <string>
    #include <iostream>
    using namespace std;
    // Function Prototypes
    void getPassword();
    bool validatePassword(char*, int&, const int);
    void convertChars(char*, int);
    //void reversePassword(char*, int);
    
    void main()
    {
    	getPassword();
    }
    
    void getPassword()
    {
    	int len = 0;
    	const int SIZE = 30;
    	char password[SIZE];
    	bool valid = false;
    	cout << "\nPlease enter a password that is between 6 and " << SIZE-1 << endl;
    	cout << "characters long, has at least one uppercase letter,\n";
    	cout << "one lowercase letter, and at least one digit. ";
    	cin.getline(password, SIZE);
    	len = strlen(password);
    	while(!(len >= 6 && len <= 29))
    	{
    		cout << "\nPassword must be between 6 and 29 characters.\n";
    		cout << "Please reenter your password. ";
    		cin.getline(password, SIZE);
    		len = strlen(password);
    	}
    	if(len >= 6 && len <= 29)
    	{
    		valid = validatePassword(password, len, SIZE);
    	}
    
    	while(!valid)
    	{
    		cout << "\nEntered password is not in the proper format.\n";
    		cout << "\nPlease enter a password that is between 6 and " << SIZE-1 << endl;
    		cout << "characters long, has at least one uppercase letter,\n";
    		cout << "one lowercase letter, and at least one digit. ";
    		cin.getline(password, SIZE);
    		len = strlen(password);
    	while(!(len >= 6 && len <= 29))
    	{
    		cout << "\nPassword must be between 6 and 29 characters.\n";
    		cout << "Please reenter your password. ";
    		cin.getline(password, SIZE);
    		len = strlen(password);
    	}
    	if(len >= 6 && len <= 29)
    	{
    		valid = validatePassword(password, len, SIZE);
    	}
    	}
    		
    
    	/*
    	while(!valid)
    	{
    		getPassword();
    		cout << "\nEntered password is not in the proper format.\n";
    		cout << "Please reenter a password that is between 6 and " << SIZE-1 << endl;
    		cout << "characters long, has at least one uppercase letter,\n";
    		cout << "one lowercase letter, and at least one digit. ";
    		cin.getline(password, SIZE);
    		bool valid = validatePassword(password, len);
    		
    	}	*/
    
    	if(valid)
    	{
    		convertChars(password, len);
    	}
    	//reversePassword(password, SIZE);
    
    	
    }
    //********************************************************************************************************
    
    bool validatePassword(char* pass, int& len, const int s)
    {
    	//const int s = 30;
    	bool hasUpper = false;
    	bool hasLower = false;
    	bool hasDigit = false;
    	bool length = false;
    	bool valid = false;
    	if(len >= 6 && len <= 29)
    		length = true;
    	//len = strlen(pass);
    	/*
    	while(!(len >= 6) && !(len <= 29))
    	{
    		cout << "Password must be between 6 and 29 characters.\n";
    		cout << "Please reenter your password. ";
    		cin.getline(pass, s);
    	}
    	*/
    
    	for(int count = 0; count < len; count++)
    	{
    		if(isupper(pass[count]))
    			hasUpper = true;
    		if(islower(pass[count]))
    			hasLower = true;
    		if(isdigit(pass[count]))
    			hasDigit = true;
    	}
    	if(hasUpper && hasLower && hasDigit && length)
    	{
    		valid = true;
    	}
    	/*
    
    	while(!valid)
    	{
    		cout << "\nEntered password is not in the proper format.\n";
    		getPassword();
    	} */
    
    
    	return valid;
    }
    //********************************************************************************************************
    
    void convertChars(char* pass, int length)
    {	
    
    	for(int count = 0; count < length; count++)
    	{ 
    		if(isupper(pass[count]))
    		{		
    			pass[count] = tolower(pass[count]);	
    
    		}
    		else
    		{
    			pass[count] = toupper(pass[count]);
    		}
    	}
    
    	cout << "\nYour password with uppercase converted to lowercase and vice versa:\n";
    
    	cout << "\t" << pass << endl;
    		
    }
    
    
     
  2. SWario

    SWario Sergeant

    Sadly, C and C++ support for strings is pretty sparse. Try this.
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds