String test for capitalization failing to work

Sunny101 (66)
These all fail to work. I have no idea why. Any help is appreciated.


p1 is user entered char string. Another code array sets the string as 'p' if it passes the validation

Thanks,
nela

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//capital letters
	for ( int i = 0 ; i < size ; i ++ )
	{
		if ( isupper(p1[i]) )
		{
			code [1] = 'P';
		}
		else
		{
			code [1] = 'F';
		}
	}

	// password has digits
	for ( int i = 0 ; i < size ; i ++ )
	{
		if (isdigit( p1[i]) )
		{
			code [2] = 'P';
		}
		else
		{
			code [2] = 'F';
		}
	}

	//password has symbols 
	for ( int i = 0 ; i < size ; i ++ )
	{
		if ( p1[i] >= 33 && p1[i] <= 45 ||  p2[i] >=60 && p1[i] <= 63 )
		{
			code [3] = 'P';
		}
		else
		{
			code [3] = 'F';
		}
	}
naraku9333 (919)
Try this
1
2
3
4
5
6
7
8
9
//capital letters
        code [1] = 'F';
	for ( int i = 0 ; i < size ; i ++ )
	{
		if ( isupper(p1[i]) )
		{
			code [1] = 'P';
		}		
	}
your way will only work if last char is uppercase.

Edit: Same goes for the other two tests.
Last edited on
Sunny101 (66)
But if i starts at 0, would it not start searching the char array from the beginning?
Sunny101 (66)
It works perfectly now! Thanks! I'm still confused on why it works when code[] is moved up. Could you elaborate?
naraku9333 (919)
Initially set it to false, then set it true ONLY if the condition is met at least once.
MikeyBoy (175)
The problem was that code[1] was only ever storing the most recent comparison result.

Imagine your string was: "aBc".

The first time through the loop, it sees 'a' is not upper-case, and sets code[1] to be 'F'.
The second time, it sees 'B' is upper-case, and sets code[1] to be 'T'.
The third time, it sees 'c' is lower-case, and sets code[1] to be 'F'.

The final value of code[1] is therefore 'F'.

If you used a debugger - or even just got your code to output some debugging information to stdout, you would have been able to see the changing value of code[1] for yourself, and seen what was happening.
Registered users can post here. Sign in or register to post.