UAB – 2005: Problem 5: Valid Identifier Checker


An identifier in Java, C++ and most other programming languages must begin with a letter and then may be followed by any number of letters or digits.

It is possible that underscores (_) will also appear, but only in the middle and never two consecutively.

Write a program to read a string and output whether it is a valid or invalid identifier. Each string will be 10 characters or less in size.

Example 1:
Enter id: UAB_HSPC
Answer: UAB_HSPC is a valid identifier

Example 2:
Enter id: a_b_c__2
Answer: a_b_c__2 is not a valid identifier


Enter id: a
Answer: a is a valid identifier
Enter id: AAAAAA
Answer: AAAAAA is a valid identifier
Enter id: 4AAAAA
Answer: 4AAAAA is not a valid identifier
Enter id: _AAAAA
Answer: _AAAAA is not a valid identifier
Enter id: aAAA
Answer: aAAA is a valid identifier
Enter id: aaaaa
Answer: aaaaa is a valid identifier
Enter id: abc_
Answer: abc_ is not a valid identifier
Enter id: abc__
Answer: abc__ is not a valid identifier
Enter id: _abc_
Answer: _abc_ is not a valid identifier
Enter id: a__bc
Answer: a__bc is not a valid identifier
Enter id: hello9
Answer: hello9 is a valid identifier


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>
#include <cstring>

using namespace std;

int main (int argc, char *argv [])
{
    char string [11];
    
    printf ("Enter id: ");
    scanf ("%s", string);
    
    unsigned long length = strlen (string) - 1;
    
    // true -> if we found any letter
    bool letter = false;
    
    // checking if the first character is a capital letter 
    if ( string [0] >= 'A' && string [0] <= 'Z' )
        letter = true;
    
    // checking if the first character is a small letter 
    if ( string [0] >= 'a' && string [0] <= 'z' )
        letter = true;
    
    // true -> if there is no underscore at the end
    bool underScore = true;
    
    // checking if there is any underscore at the end 
    if ( string [length] == '_' )
        underScore = false;
    
    int flag = 0;
    
    // we have already checked the first character
    // so starting the loop from 1 
    for ( int i = 1; i <= length; i++ ) {
        
        // if we found an underscore, its a warning, flag++ 
        if ( string [i] == '_' )
            flag++;
        else
            flag = 0;
        
        // if flag == 2, we have two consecutive underscore 
        if ( flag == 2 ) {
            underScore = false;
            break;
        }
    }
    
    // if letter and uderscore both are true, then our identifier is clear
    if ( letter && underScore )
        printf ("Answer: %s is a valid identifier\n", string);
    else
        printf ("Answer: %s is not a valid identifier\n", string);
    
    return 0;
}

// @END_OF_SOURCE_CODE

Similar Problem

Write a program that will validate a password.

Password must have,
1) 8 to 12 characters
2) at least one numeric digit
3) at least one alphabet
4) must not have space or slash (/)
5) at least one Capital letter
6) starting character must not be a number

// @BEGIN_OF_SOURCE_CODE

#include <cstdio>
#include <cstring>

using namespace std;

bool isValidLength(char a [])
{
    int length = strlen (a);

    return length >= 8 && length <= 12;
}

bool hasNumericDigit(char a [])
{
    int length = strlen (a);

    for ( int i = 0; i < length; i++ ) {
        if (a [i] >= '0' && a [i] <= '9') return true;
    }

    return false;
}

bool hasAlphabet(char a [])
{
    int length = strlen (a);

    for ( int i = 0; i < length; i++ ) {
        if (a [i] >= 'a' && a [i] <= 'z') return true;
        if (a [i] >= 'A' && a [i] <= 'Z') return true;
    }

    return false;
}

bool hasSpace(char a [])
{
    int length = strlen (a);

    for ( int i = 0; i < length; i++ ) {
        if (a [i] == ' ') return true;
    }

    return false;
}

bool hasSlash(char a [])
{
    int length = strlen (a);

    for ( int i = 0; i < length; i++ ) {
        if (a [i] == '/') return true;
    }

    return false;
}

bool hasCapitalLetter(char a [])
{
    int length = strlen (a);

    for ( int i = 0; i < length; i++ ) {
        if (a [i] >= 'A' && a [i] <= 'Z') return true;
    }

    return false;
}

bool hasStartedWithNumber(char a [])
{
    int length = strlen (a);

    // does not stated with a number 
    if (length == 0) return false;

    return a [0] >= '0' && a [0] <= '9';
}

int main(int argc, const char * argv[])
{
    char a [100];
    
    gets(a);
    
    if (isValidLength(a) &&
            hasNumericDigit(a) &&
            hasAlphabet(a) &&
            !hasSpace(a) &&
            !hasSlash(a) &&
            hasCapitalLetter(a) &&
            !hasStartedWithNumber(a)) {
        
        printf ("Valid password");
    }

    return 0;
}

// @END_OF_SOURCE_CODE
// @BEGIN_OF_SOURCE_CODE

#include <cstdio>
#include <cstring>
#include <cctype>

using namespace std;

bool isValidLength(char a [])
{
    int length = strlen (a);

    return length >= 8 && length <= 12;
}

bool hasNumericDigit(char a [])
{
    int length = strlen (a);

    for ( int i = 0; i < length; i++ ) {
        if (isdigit(a [i])) return true;
    }

    return false;
}

bool hasAlphabet(char a [])
{
    int length = strlen (a);

    for ( int i = 0; i < length; i++ ) {
        if (isalpha(a [i])) return true;
    }

    return false;
}

bool hasSpace(char a [])
{
    int length = strlen (a);

    for ( int i = 0; i < length; i++ ) {
        if (isspace(a [i])) return true;
    }

    return false;
}

bool hasSlash(char a [])
{
    int length = strlen (a);

    for ( int i = 0; i < length; i++ ) {
        if (a [i] == '/') return true;
    }

    return false;
}

bool hasCapitalLetter(char a [])
{
    int length = strlen (a);

    for ( int i = 0; i < length; i++ ) {
        if (isupper(a [i])) return true;
    }

    return false;
}

bool hasStartedWithNumber(char a [])
{
    int length = strlen (a);

    // does not stated with a number 
    if (length == 0) return false;

    return isdigit(a [0]);
}

int main(int argc, const char * argv[])
{
    char a [100];

    gets(a);

    if (isValidLength(a) &&
            hasNumericDigit(a) &&
            hasAlphabet(a) &&
            !hasSpace(a) &&
            !hasSlash(a) &&
            hasCapitalLetter(a) &&
            !hasStartedWithNumber(a)) {

        printf ("Valid password");
    }

    return 0;
}

// @END_OF_SOURCE_CODE
Advertisements

8 thoughts on “UAB – 2005: Problem 5: Valid Identifier Checker

  1. Solved in Python 3.1.2:

    import re
    import string

    identifier = input(“Logging into neural net. Please provide ID: “)

    if identifier[:1] not in string.ascii_lowercase and identifier[:1] not in string.ascii_uppercase:
    print(identifier, “does not start with a legal alphabetic character. >:C”)
    exit()

    reginald = re.compile(‘__’)
    ronald = reginald.search(identifier)

    if ronald:
    print(identifier, “contains at least one _ next to another _. >__ 10:
    print(identifier, “is longer than 10 characters. D:”)
    exit()

    renard = re.compile(r’\b_|_\b’)
    roger = renard.search(identifier)

    if roger:
    print(identifier, “cannot contain underscores at its edge. >__>”)
    exit()

    print(“Identifier”, identifier, “is valid! :D”)

  2. #include
    #include

    std::string a;

    bool isUnderscore(){
    int i, length = a.length() – 1;
    for(i=1; i> a;
    if((isUnderscore()) && ((a[0] >= ‘a’ && a[0] = ‘A’ && a[0] <= 'Z') || a[0] == '_'))
    std::cout << "Answer: " << a <<" is a valid identifier\n";
    else
    std::cout << "Answer: " << a <<" is not a valid identifier\n";
    return 0;
    }

  3. #include
    #include
    int main(){
    char identifier[11];
    int letter,underscore,i,l,count=0;
    printf(“please enter an identifier: “);
    fgets(identifier,11,stdin);
    for(i=0;i<11;i++){
    if(identifier[i]=='\n'){identifier[i]='\0';}
    }
    printf("\n");
    letter=0;
    if('a'<=identifier[0]&&identifier[0]<='z'||'A'<=identifier[0]&&identifier[0]<='Z'){letter=1;}
    l=strlen(identifier)-1;
    underscore=1;
    if(identifier[l]=='_'){underscore=0;}
    for(i=1;i<=l;i++){
    if(identifier[i]=='_'){count=count+1;}
    else{count=0;}
    if(count==2){underscore=0;break;}
    }

    if(letter&&underscore){printf("%s is a valid identifier.",identifier);}
    else{printf("%s is not a valid identifier.",identifier);}
    printf("\n");
    return 0;
    }

  4. a program to validate a password:

    #include
    #include
    int main(){
    char password[13];
    int length,digit,alphabet,space,slash,CapitalLetter,StartingCharacter,i;
    printf(“please enter a password containing between 8 and 12 characters: “);
    fgets(password,13,stdin);
    for(i=0;i<13;i++){
    if(password[i]=='\n'){password[i]='\0';}}
    length=strlen(password)-1;
    digit=0;
    for(i=0;i<length;i++){if('0'<=password[i]&&password[i]<='9'){digit=1;break;}}
    alphabet=0;
    for(i=0;i<length;i++){if('z'<=password[i]&&password[i]<='z'||'A'<=password[i]&&password[i]<='Z'){alphabet=1;break;}}
    space=1;
    slash=1;
    for(i=0;i<length;i++){if(password[i]==' '||password[i]=='/'){space=0;slash=0;break;}}
    CapitalLetter=0;
    for(i=0;i<length;i++){if('A'<=password[i]&&password[i]<='Z'){CapitalLetter=1;break;}}
    StartingCharacter=1;
    if('0'<=password[0]&&password[0]8&&digit&&alphabet&&space&&slash&&CapitalLetter&&StartingCharacter)
    {
    printf(“%s is a valid password.”,password);}
    else{
    printf(“%s is an invalid password.”,password);
    }
    return 0;
    }

  5. using System;
    namespace CheakPassword
    {
    class Program
    {

    static void Main(string[] args)
    {
    int CountNum = 0, CountChar = 0,CountCapitalChar=0,CountSpecialChar=0;
    Console.Write(” Enter Password To Cheaked : “);
    string Password = Console.ReadLine();

    if (Password.Length>=8 && Password.Length<=12)
    {
    if (Password[0] 57)
    {
    for (int i = 0; i = 48 && Password[i] = 65 && Password[i] = 97 && Password[i] = 97 && Password[i] = 32 && Password[i] <= 47)
    CountSpecialChar++;

    }
    }

    }

    if (CountNum == 0 || CountChar == 0 || CountCapitalChar == 0 || CountSpecialChar != 0)
    Console.WriteLine(" Error : Enter New Password");

    else
    Console.WriteLine("************* Correct Password ***************");

    }
    }
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s