what is your mobile number?

These days Bechan Chacha is depressed because his crush gave him a list of mobile numbers some of them are valid and some of them are invalid. Bechan Chacha has a special power that he can pick his crush number only if he has a valid set of mobile numbers. Help him to determine the valid numbers.

You are given a string "S" and you have to determine whether it is a Valid mobile number or not. Mobile number is valid only if it is of length 10, consists of numeric values and it shouldn't have prefix zeroes.

Example:

Input:  s ="1234567890"
Output: YES

Approach

C++

#include <bits/stdc++.h>
using namespace std;

void checkMobileNumber(string s)
{
    if (s.size() != 10)
        cout << "NO\n";
    else
    {
        if (s[0] != '0' && s[0] >= '1' && s[0] <= '9')
        {
            long long i = 1, flag = 0;
            while (i < 10)
            {
                if (s[i] >= '0' && s[i] <= '9')
                    i++;
                else
                {
                    flag = 1;
                    break;
                }
            }
            if (flag == 1)
                cout << "NO\n";
            else
                cout << "YES\n";
        }
        else
            cout << "NO\n";
    }
}
int main()
{

    string s = "1234567890";

    checkMobileNumber(s);

    return 0;
}


No comments:

Post a Comment