### Scanning the number of letters and numbers in a string

Hello,

I have an assignment to write a code that will ask for a licence plate and in return will answer whether it is a regular, a custom or incorrect. Since I live in Estonia, where regular plates are 3 or 2 numbers followed by 3 letters and custom plates have up to 9 characters where there has to be atleast 1 number and 1 letter.

What I am having trouble with is: How can i make the program count the numbers and letters separetly from a line?

For example i ask for a licence plate and the user enters 851TFF and it should understand that it's a regular and give the respective answer, but if something random is inserted or it goes over the limit like 854 then it would say that it is invalid.

Christo
Do you need something like:
 ``123456789101112131415`` ``````std::string line; std::cin >> line; int digits = 0; int letters = 0; int other = 0; for(char c: line) { if(isdigit(c)) ++digits; else if(isalpha(c)) ++letters; else ++other; } if (other != 0) std::cout << "junk symbols in plate number";``````
closed account (iAk3T05o)
@minipaa: could you explain your code?
 ``12345678910111213141516`` ``````std::string line; //Declare variable called line of type of std::string std::cin >> line; //Read input from user into line variable int digits = 0; //Create counters int letters = 0; //For different kind int other = 0; // of symbors for(char c: line) { //Loop for each symbol in line if(isdigit(c)) //If currend symbol is a digit... ++digits; //...increase number of digits else if(isalpha(c)) //If it is a letter... ++letters; //Increase amount of letters else //If it is not a digit or a letter ++other; //Then it is some other symbol } //Now in digits variable we have amount of digits, number of letters in letters, and other symbols in other if (other != 0) //Output message if there is anything aside from digits and letters in entered License Pate std::cout << "junk symbols in plate number";``````
Topic archived. No new replies allowed.