Extract Phone Numbers from Text in C, C++, Java & Python – Code with Explanation & Examples in Short and Simple

   

C Program

#include <stdio.h>
#include <string.h>
int main() {
    char t[500]; fgets(t, 500, stdin);
    for (int i = 0; t[i]; i++)
        if (sscanf(&t[i], "%10[0-9]", t) == 1 && strlen(t) == 10)
            printf("%s\n", t), i += 9;
}

C Output

Input:
Call me at 9876543210 or 1234567890 soon.

Output:
9876543210
1234567890



C++ Program

#include <iostream>
#include <regex>
using namespace std;
int main() {
    string t; getline(cin, t);
    regex r("\\b\\d{10}\\b"); smatch m;
    while (regex_search(t, m, r)) {
        cout << m[0] << endl;
        t = m.suffix();
    }
}

C++ Output

Input:
Text me at 8123456789 now or 9988776655 later.

Output:
8123456789
9988776655



JAVA Program

import java.util.regex.*;
import java.util.Scanner;
public class Main {
    public static void main(String[] a) {
        String t = new Scanner(System.in).nextLine();
        Matcher m = Pattern.compile("\\b\\d{10}\\b").matcher(t);
        while (m.find()) System.out.println(m.group());
    }
}

JAVA Output

Input:
Emergency: 9112345678 or 8001234567

Output:
9112345678

8001234567



Python Program

import re
print(*re.findall(r'\b\d{10}\b', input()), sep='\n')

Python Output

Call 7778889999 or 6665554444 today!


Output: 7778889999
6665554444



In-Depth Explanation
Example
The input in the Python version is:
Call 7778889999 or 6665554444 today!
The regex \b\d{10}\b matches all precisely 10-digit numbers enclosed by word boundaries (\b). Therefore, it captures both numbers neatly.

In Python, Java, and C++, regular expressions (regex) lift the heavy work. In C, we emulate with sscanf and manual scanning.

Real-Life Analogy
Think of extracting phone numbers from a message or document containing phone numbers. You need to bring in only the valid 10-digit mobile numbers out of that chaos. This is what customer support software, contact management software, and CRMs do on a daily basis — extracting numbers automatically from messages, notes, or emails.

Why It Matters
Extraction of phone numbers is extremely helpful in:

Contact scraping tools

CRM applications

Form processing

SMS gateways

Document parsing systems

It's an essential building block in data scraping, lead generation, and cognitive automation.

Learning Insights
You will learn:

Regular expressions to match pattern

File-free input processing and string searching

Word boundaries (\\b) to prevent matching longer strings such as 123456789012

Optimized looping and string slicing methods

In lower-level languages such as C, this also reinforces your understanding of parsing strings by hand using sscanf, indexing, and string manipulation.

Interview & Project Relevance
This problem occurs frequently in:

Backend data cleaning operations

Form input validation

Resume parsing

Real-time communication software

Data mining and lead generation applications

Interviewers test it in order to evaluate:

Your regex comfort

Your ability to extract significant data

Your string logic and input parsing practical application

Extracting phone numbers from text is a realistic, everyday coding problem that finds application in automation, communication systems, and data extraction pipelines. The above code examples illustrate how to do it in the shortest, most elegant manner using C, C++, Java, and Python with a distinct input/output. Learning this logic enables you to create smarter apps that can extract useful information out of disorganized text — an invaluable skill in today's data-centric world.