Solved programming problem

Identify String Pattern in C++.

Source code

Pattern Type:

 

1st char cant be a Digit.

all alphabet are in Capital

must have one '@'

next char after '@' must be 'N'

after N one digit with the range of [1-3]

then range [1-999]

 if the input follows those condition then it is accepted.

Code:

#include<bits/stdc++.h>

#include<string>

using namespace std;

int  checklast(string input,int index)

{

    if(input[index]!='0' && input[index+1]!='0' && input[index]+2!='0')

    {

        if(input[index]>='0' && input[index]<='9')

        {

            if(input[index+1]>='0' && input[index+1]<='9')

            {

                if(input[index]>='0' && input[index]<='9')

                {

                    cout<<"Accepted"<<endl;

                    return 0;

                }

            }

        }

    }

    cout<<"Not Accepted"<<endl;

    return 0;

}

int checkN(string input,int index)

{

    if(input[index+1]!='N')

    {

        cout<<"Not Accepted"<<endl;

        return 0;

    }

    else

    {

        if(input[index+2]=='1' || input[index+2]=='2' || input[index+2] =='3' )

        {

            checklast(input,index+3);

        }

        else

        {

            cout<<"Not Accepted"<<endl;

            return 0;

        }

    }

}

int checkalpha(string input)

{

    int count=0;

    int index=0;

    for(int i=0; i<input.size(); i++)

    {

        if(input[i]=='@')

        {

            count++;

            index=i;

        }

    }

    if(count==1)

    {

        for(int i=0; i<index; i++)

        {

            if(!(input[i]>='A' && input[i]<='Z'))

            {

                cout<<"Not Accepted"<<endl;

                return 0;

            }

        }

    }

    else

    {

        cout<<"Not Accepted"<<endl;

        return 0;

    }

    checkN(input,index);

}

int checkDigit(string input)

{

    if(input[0]>='A' && input[0]<='Z')

    {

        checkalpha(input);

    }

    else

    {

        cout<<"Not Accepted"<<endl;

        return 0;

    }

}

int main()

{

    string input;

    while(1)

    {

        cout<<"Input: ";

        cin>>input;

        checkDigit(input);

    }

    return 0;

}

Output

INPUT: SABIT@N1234

ACCEPTED

INPUT:SABIT@N1000

NOT ACCEPTED

INPUT:0SABITN

NOT ACCEPTED

Join the discussion

Add your comment