The string is a sequence of characters. In java, objects of String are immutable. Immutable means that once an object is created, it’s content can’t change. Complete traversal in the string is required to find the total number of digits in a string.
Input : string = GeeksforGeeks password is : 1234" Output: Total number of Digits = 4 Input : string = "G e e k s f o r G e e k" Output: Total number of Digits = 4
- Create one integer variable and initialize it with 0.
- Start string traversal.
- If the ASCII code of character at the current index is greater than or equals to 48 and less than or equals to 57 then increment the variable.
- After the end of the traversal, print variable.
Below is the implementation of the above approach:
Total number of Digits = 4
Time Complexity: O(N), where N is the length of the string.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.