6 Replies - 2783 Views - Last Post: 07 November 2011 - 07:57 PM Rate Topic: -----

#1 Queen_Aries  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 07-September 11

How to search for upper/lowercase letters in a string

Posted 07 November 2011 - 06:22 PM

Good Evening,

I have an assignment to create a program that asks the user to create a password. The password must be at least 6 characters long, contain at least one uppercase and one lowercase letter, and contain at least one digit. I have written a program, but no matter what the password is, it does not recognize upper or lower case letters. For example, if I run my program and enter 2RxteRys. I will get the message your password is invalid. Password must contain an uppercase letter. I cannot figure out what I did or did not do in my validation of the password to keep getting this error. This is a C++ program and the focus of the asssignment is on characters, strings, and the string class. Here is what I have so far:

#include "stdafx.h"
#include <iostream>
#include <cctype>
#include <cstring>
#include <cstdlib>
using namespace std;

//Function prototype for validating the password
bool validate(char[], int);

int _tmain(int argc, _TCHAR* argv[])
{
	const int SIZE = 25;		//Array size
	char password[SIZE];		//To hold the password
	char choice;				//To hold user's choice to restart
	
	

	do
	{

		//Get the user's password
		cout << "Please enter a password.\n";
		cout << "Password must be six characters long.\n";
		cout << "Password must include at least one uppercase and one lowercase letter.\n";
		cout << "Password must include at least one number.\n";
		cin.getline(password, SIZE);

		//Validate the password
		if (validate(password, SIZE))
			cout << "Your password has been accepted.\n";
		else
			cout << "Your password is invalid. Please try again.\n";
		
		cout << "Would you like to try again? Y or N?\n";
		cin >> choice;

		//Validate choice
		while (toupper(choice) != 'Y' && toupper(choice) != 'N')
		{
			cout << "Please enter Y or N?\n";
			cin >> choice;
		}

	}
	while (toupper(choice) == 'Y');
	
	return 0;
	}

//Function Definition
bool validate(char password[], int size)
{
	int count;		//Loop counter
	const int SIZE = 6;
	int length;
	length = strlen(password);
	
	//Test for length.
	if (length < SIZE)
	{
		cout << "Your password must be at least 6 characters long.\n";
		return false;
	}
	
	//Test for uppercase
	for (count = 0; count < SIZE; count++)
	{
		if (!isupper(password[count]))
		{
			cout << "Your password must have at least one uppercase letter.\n";
			return false;
		}
	}

	//Test for lowercase
	for (count = 0; count < SIZE; count++)
	{
		if (!islower(password[count]))
		{
			cout << "Your password must have at least one lowercase letter.\n";
			return false;
		}
	}

	//Test for digit
	for (count = 0; count < SIZE; count++)
	{
		if(!isalnum(password[count]))
		{
			cout << "Your password must have at least one number.\n";
			return false;
		}
	}
	return true;
}



I'd appreciate any help/hints I can get. Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: How to search for upper/lowercase letters in a string

#2 buffalobill  Icon User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • Posts: 188
  • Joined: 08-July 08

Re: How to search for upper/lowercase letters in a string

Posted 07 November 2011 - 07:05 PM

What is line 69 and 79 doing?
I may be wrong but they look to me as though they expect all characters to be upper case and then all characters to be lower case. If this is right you need to have a separate counting variable in each loop to register a minimum of one upper case char and a minimum of one lower case char before assigning true and thereby achieving validation. I suppose this should apply to the digit also.
Was This Post Helpful? 1
  • +
  • -

#3 Queen_Aries  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 07-September 11

Re: How to search for upper/lowercase letters in a string

Posted 07 November 2011 - 07:11 PM

View Postbuffalobill, on 07 November 2011 - 09:05 PM, said:

What is line 69 and 79 doing?
I may be wrong but they look to me as though they expect all characters to be upper case and then all characters to be lower case. If this is right you need to have a separate counting variable in each loop to register a minimum of one upper case char and a minimum of one lower case char before assigning true and thereby achieving validation. I suppose this should apply to the digit also.


Essentially, that is what they are doing, but I needed it search for the minimum but I couldn't figure out how to do it.
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,437
  • Joined: 25-December 09

Re: How to search for upper/lowercase letters in a string

Posted 07 November 2011 - 07:12 PM

In your validate() function you define SIZE as:
bool validate(char password[], int size)
{
	const int SIZE = 6;
	int length;
	length = strlen(password);

	//Test for uppercase
	for (count = 0; count < SIZE; count++)


Yet the length of the string could be a different size. Shouldn't the for statement be checking all of the characters not just the first 6? What is the purpose of the size parameter?

Also why are you using character strings instead of std::strings? Using std::strings would make this much easier because a std::string knows it's size.

Jim

This post has been edited by jimblumberg: 07 November 2011 - 07:13 PM

Was This Post Helpful? 0
  • +
  • -

#5 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1432
  • View blog
  • Posts: 4,968
  • Joined: 19-February 09

Re: How to search for upper/lowercase letters in a string

Posted 07 November 2011 - 07:13 PM

Hi, here you loop over each character in the array, the string is only a part of the array.

For each character you check whether it is uppercase or not. If it is a valid lowercase character you return false.

66	    //Test for uppercase
67	    for (count = 0; count < SIZE; count++)
68	    {
69	        if (!isupper(password[count]))
70	        {
71	            cout << "Your password must have at least one uppercase letter.\n";
72	            return false;
73	        }
74	    }



You need to the program to remember if the string contains a uppercase letter or not
Possibly you could have a variable that you set to true if you find an uppercase letter.

isalnum is true for letters and numbers, isdigit is true for numbers.
Was This Post Helpful? 0
  • +
  • -

#6 Queen_Aries  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 07-September 11

Re: How to search for upper/lowercase letters in a string

Posted 07 November 2011 - 07:46 PM

Hi Jim,

I used a const size of 6 because the password must be at least 6 characters long. The part of the code is to make sure the user doesn't enter anything less than 6 characters.

Frankly, we are just learning about std::strings (long story). But basically, I was following the examples in the textbook, and they did not use std::strings.


View Postjimblumberg, on 07 November 2011 - 09:12 PM, said:

In your validate() function you define SIZE as:
bool validate(char password[], int size)
{
	const int SIZE = 6;
	int length;
	length = strlen(password);

	//Test for uppercase
	for (count = 0; count < SIZE; count++)


Yet the length of the string could be a different size. Shouldn't the for statement be checking all of the characters not just the first 6? What is the purpose of the size parameter?

Also why are you using character strings instead of std::strings? Using std::strings would make this much easier because a std::string knows it's size.

Jim

Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is offline

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,437
  • Joined: 25-December 09

Re: How to search for upper/lowercase letters in a string

Posted 07 November 2011 - 07:57 PM

Quote

I used a const size of 6 because the password must be at least 6 characters long.

What happens if the string is longer than 6 characters. In main you allow for more than 6 characters to be entered. You should probably be using the length of the string in your "for loop" not some arbitrary number.

Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1