Thread: validate alphabets and numbers

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    33

    validate alphabets and numbers

    I have use the below to validate my alphabets but it seem it does not work when i input 1234abc, it will accept it into my files. Anything i do wrong? I require to validate all the character to be alphabets, btw if all them njumbers, they will not be accepted as planned.

    PHP Code:
    char val_str(char text[])
    {
        
    char status;
        
    int x=0;

        while (
    text[x] != '\0')
        {
            if (
    toupper(isalpha (text[x]) ==0))
                
    status 'N';
            else
                
    status ='Y';
            ++
    x;
        }
        return 
    status;


  2. #2
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    Your function val_str() returns a status 'Y' or 'N' based on the last character in your string passed to it as 'text'. Look closely, Though you are running the loop for the entire string, only the status for the last character is returned. To avoid this, break once you find a non-alphabet.

    Code:
    char val_str(char text[])
    {
        char status;
        int x=0;
    
        while (text[x] != '')
        {
            if (toupper(isalpha (text[x]) ==0)) {
                status = 'N';
                break;
            }
            else
                status ='Y';
            ++x;
        }
        return status;
    }

  3. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    More elegant would be to introduce a test-variable. Just like this:

    Code:
    typedef enum {FALSE, TRUE} boolean;
    
    char val_str (char text [])
    {
        char status = 'Y';
        int x = 0;
        boolean invalid_char = FALSE;
    
        while (text[x] != '' && invalid_char == FALSE)
        {
            if (toupper (isalpha (text [x++]) == 0)) {
                status = 'N';
                invalid_char = TRUE;
            }
        }
    
        return status;
    }
    Or just check status.

    Code:
    typedef enum {FALSE, TRUE} boolean;
    
    char val_str (char text [])
    {
        char status = 'Y';
        int x = 0;
        boolean invalid_char = FALSE;
    
        while (text[x] != '' && status == 'Y')
        {
            if (toupper (isalpha (text [x++]) == 0))
                status = 'N';
        }
    
        return status;
    }

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    OK, a few points here I think must be mentioned...

    >while (text[x] != '' .....
    you cannot do this. A string constant cannot be zero in length. If you mean ' ', then your loop is still wrong. It should be
    >while (text[x] != '\0'


    >if (toupper (isalpha (text [x++]) == 0))
    What exactly are you trying to test here?? Whatever, it's not the best To test if a char is alpha, just call isalpha(). You don't need toupper().

    Here's a simple version. myfunc returns 0 if the string is all characters, 1 if not.
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    int myfunc(char *s)
    {
    	for ( ; *s != '\0'; s++)
    	{
    		if (isalpha(*s) == 0)
    			return 1;
    	}
    	
    	return 0;
    }
    
    int main(void)
    {
    	char buf[] = "ThisIsCool";
    	
    	if (myfunc(buf))
    		printf ("NOT Alpha\n");
    	else
    		printf ("IS Alpha\n");
    		
    	return 0;
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    33
    hi Hammer, just noted your reply today

    that problem was solved, thanks too all too

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Allow them to enter numbers only
    By d1343 in forum C Programming
    Replies: 4
    Last Post: 02-15-2009, 03:49 PM
  2. Question regarding random numbers
    By girish1026 in forum C Programming
    Replies: 1
    Last Post: 01-03-2009, 03:44 AM