add a,b,c & d (into r) and check for r!=4

This is a discussion on add a,b,c & d (into r) and check for r!=4 within the C Programming forums, part of the General Programming Boards category; I have my program mostly figured out, but the teacher wants us to "add a,b,c & d (into r) and ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    5

    add a,b,c & d (into r) and check for r!=4

    I have my program mostly figured out, but the teacher wants us to "add a,b,c & d (into r) and check for r!=4". How do would I do this?

    Also, why do my last statements only work if i return 1 for if it does not work?

    Thanks.

    Code:
    #include <stdio.h>
    #pragma warning(disable:4996)
    #include <string.h>
    #include <ctype.h>
    
    void explanation();
    
    /* Set up the check functions to return a 1 if the password passes */
    /* and a zero if not! */
    
    int length_check(char[]);
    int no_symbols(char[]);
    int at_least_one_digit(char[]);
    int at_least_one_letter(char[]);
    
    int main()
    {
    	char password[20];
    	int a, b, c, d, r;
        
    	do
    	{
            printf("Enter your test password\n");
    		scanf("%s",password);
    		a = length_check(password);
    		{
    		if(a==0)
    			printf("Must consist of atleast 8 characters or digits\n");
    		}
    		b = no_symbols(password);
    		{
    		if(b==0)
    			printf("Must consist of only alphabetic characters and digits\n");
    		}
    		
    		c = at_least_one_digit(password);
    		{
    		if(c==1)
    			printf("Must consist of atleast one digits\n");
    		}
    
    		d = at_least_one_letter(password);
    		{
    		if(d==1)
    			printf("Must consist of atleast one alphabetic character\n");
    		}
    	}while(r!=1); /*Hint: add a,b,c & d (into r) and check for r!=4 */
    
    	printf("Legal password!\n");
    	scanf("%s",password);
    
    	return 0;
    }
    
    int length_check(char p[])
    {	int i=0,w=0;
    	
    	while(p[i] != '\0')
    	{
    		if((int)strlen(p)!=0)
    		       w++;
    		
    		i++;
    	}
    
    	if(w>=7)
    		return 1;
    	else
    		return 0;
    
    }
    
    
    int no_symbols(char p[])
    {   int i=0,w=0;
    	
    	while(p[i] != '\0') /* Strings are terminated with the null character */
    	{
    		if(ispunct(p[i])!=0)/*I suggest a google search for ispunct */
    		       w++;
    		
    		i++; /* Move through the string */
    	}
    
    	if(w==0)
    		return 1;
    	else
    		return 0;
    
    }
    
    int at_least_one_digit(char p[])
    {   int i=0,w=0;
    	
    	while(p[i] != '\0')
    	{
    		if(isdigit(p[i]))
    		       w++;
    		
    		i++;
    	}
    
    	if(w==0)
    		return 1;
    	else
    		return 0;
    
    }
    
    int at_least_one_letter(char p[])
    {   int i=0,w=0;
    	
    	while(p[i] != '\0')
    	{
    		if(isalpha(p[i])!=0)
    		       w++;
    		
    		i++;
    	}
    
    	if(w==0)
    		return 1;
    	else
    		return 0;
    
    }

  2. #2
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    length_check() can be rewritten as follows.
    Code:
    {
        int i = 0;
        while( *(p + i) )    // terminate on null-character
        {
            ++i;
        }
    
        return w >= 7 ? 1 : 0;    // if w >= 7 then 1 else 0
    }
    You can do a similar rewrite of others, but they are otherwise correct.

    You never initialize r to any particular value before making the comparison. It has a junk value that is almost guaranteed to be everything but 1.

    I suppose what your teacher meant by "add a,b,c & d (into r) and check for r!=4" is that you should return 1 if a test passes, and r will only be equal to 4 if all tests pass. He wants your return values to be consistent; 1 for success, 0 for failure.

    Like this, maybe. Just remember to reset r to zero at loop start.
    Code:
    r += length_check( password );

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    5
    thanks for your reply. I got it figured out shortly after posting. You were correct. The teacher wanted us to check for 1 and if R was = to 4 then the password was a success.

  4. #4
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    That #pragma preprocessor looks foreign. I assume it silences a specific warning?

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Quote Originally Posted by Babkockdood View Post
    That #pragma preprocessor looks foreign. I assume it silences a specific warning?
    That will silence the warning about using deprecated functions.
    bit∙hub [bit-huhb] n. A source and destination for information.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21