How to find out no. digits present in a alphanumeric string entered by a user? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How to find out no. digits present in a alphanumeric string entered by a user?

26th Sep 2017, 5:17 PM
Aditya Kishan
Aditya Kishan - avatar
2 Answers
+ 2
Use Regular Expressions. Because i dont know in which language you are code i cant search for an example in your language. but here is a article what are regulr expressions: https://en.wikipedia.org/wiki/Regular_expression a small java example but its hard to understand if you never heard of regexp or dont use java: import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexMatches { public static void main( String args[] ) { // String to be scanned to find the pattern. String line = "This order was placed for QT3000! OK?"; String pattern = "(.*)(\\d+)(.*)"; // Create a Pattern object Pattern r = Pattern.compile(pattern); // Now create matcher object. Matcher m = r.matcher(line); if (m.find( )) { System.out.println("Found value: " + m.group(0) ); System.out.println("Found value: " + m.group(1) ); System.out.println("Found value: " + m.group(2) ); }else { System.out.println("NO MATCH"); } } } This will produce the following result − Output Found value: This order was placed for QT3000! OK? Found value: This order was placed for QT300 Found value: 0
27th Sep 2017, 7:32 AM
R4xx4r
R4xx4r - avatar
+ 2
I want it to be in c++. I don't know java. Yeah i should have mentioned it before!! 😀
27th Sep 2017, 8:17 AM
Aditya Kishan
Aditya Kishan - avatar