C Program
#include <stdio.h> #include <ctype.h> int main() { char str[100]; int count = 0; printf("Enter a string: "); fgets(str, sizeof(str), stdin); for (int i = 0; str[i] != '\0'; i++) { if (isdigit(str[i])) { count++; } } printf("Total digits in string: %d\n", count); return 0; }
C Output
Input:
Hello123World45
Output:
Total digits in string: 5
C++ Program
#include <iostream> #include <cctype> using namespace std; int main() { string str; int count = 0; cout << "Enter a string: "; getline(cin, str); for (char c : str) { if (isdigit(c)) { count++; } } cout << "Total digits in string: " << count << endl; return 0; }
C++ Output
Input:
MyPass2025Code
Output:
Total digits in string: 4
JAVA Program
import java.util.Scanner; public class CountDigitsInString { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter a string: "); String str = sc.nextLine(); int count = 0; for (int i = 0; i < str.length(); i++) { if (Character.isDigit(str.charAt(i))) { count++; } } System.out.println("Total digits in string: " + count); } }
JAVA Output
Input:
RoomNo42Block7
Output:
Total digits in string: 3
Python Program
s = input("Enter a string: ") count = sum(1 for c in s if c.isdigit()) print("Total digits in string:", count)
Python Output
Input:
Python3IsCool2025
Output:
Total digits in string: 6
Explanation
Example
If the input string is "Welcome2025ToMSBTE", the string contains regular characters along with numbers. Scanning character by character, we determine which are digits (2, 0, 2, 5) and tally them up. The answer is 4. The routine is simple: iterate over the string, test each character with a digit-testing function (isdigit in C, C++, Python or Character.isDigit in Java), and maintain a counter.
Real-Life Analogy
Suppose you are reading a sentence in which numbers are embedded within words, such as "Your OTP is 4729 and is valid for 10 minutes." If someone asked you to count the number of numbers in the sentence, you would not count letters and symbols but only numeric digits. Likewise, the program only filters the digits and counts them. It's similar to scanning a mixed shopping list with both the item name and quantity on there—your role is just to write down the numeric quantities.
Why It Matters
Digit counting in a string may seem easy, but it has real-world applications. For instance, while validating form input fields like phone numbers, Aadhaar numbers, or passwords, you might need to validate whether the input has digits. In natural language processing, digit counting aids in extracting numeric information concealed in text messages. In cyber security, systems check whether passwords have a minimum of one digit, and digit counting helps them meet strong password policies.
Learning Insights
This exercise instructs newbies to handle strings beyond trivial printing. It teaches character categorization, conditional examination, and iteration through data. It's also a good introduction to library methods such as isdigit() or Character.isDigit(). Most importantly, it illustrates how small problems such as digit counting can be generalized into bigger problems such as text parsing and data retrieval.
Interview Use Case
In programming interviews, these types of questions determine how you can manipulate strings and identify patterns. The interviewer may sometimes add an extension to the question by asking not only for the count but also for extracting the digits, constructing the number, or adding them up. For example, with "abc123x4", you might be asked to return 127. This development assists interviewers in evaluating problem-solving and code flexibility.
Real-World Applications
Banking Systems: Picking numeric information such as account numbers from user input.
E-commerce: Parsing product codes with numbers.
Chatbots: Finding dates, prices, or IDs embedded in messages.
Data Cleaning: Deleting or sanitizing numeric data in text fields.
SEO Optimized Closing Paragraph
One of the most frequent string manipulation issues in programming is counting digits in a string. It helps beginners understand how to traverse strings, use character-checking methods, and apply logic in a simple but effective way. This problem is frequently asked in coding interviews, online coding challenges, and exams because it tests fundamental skills like iteration, condition checking, and text parsing. Learning to count digits from a string in C, C++, Java, and Python lays a solid foundation for even more complex text-processing issues like phone number validation, extracting numeric values from logs, and security checks in real-world applications.
Social Plugin