C Examples

WAP in C to Print IP addresses found in text!

The Program in C to Print IP addresses found in the text is given below:

#include <stdio.h>
#include <string.h>
#include <regex.h>

int main() {
    char text[256];
    printf("Enter text: ");
    fgets(text, 256, stdin);

    // Regular expression to match IP addresses
    regex_t regex;
    regcomp(&regex, "([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3})", REG_EXTENDED);

    // Execute regular expression
    regmatch_t match[1];
    if (regexec(&regex, text, 1, match, 0) == 0) {
        printf("Found IP address: %.*s\n", match[0].rm_eo - match[0].rm_so, text + match[0].rm_so);
    } else {
        printf("No IP address found in text\n");
    }

    regfree(&regex);
    return 0;
}

Output:

Enter text: dont know
No IP address found in text


Pro-Tips💡

This is a C program that uses regular expressions to match and extract IP addresses from a given string of text.

The program prompts the user to enter text, uses the fgets() function to read the text entered by the user, and stores it in a char array called “text”.


The program then creates a regular expression object “regex” using the regcomp() function, which takes the regular expression pattern for matching IP addresses and a flag indicating that the regular expression should be treated as an extended regular expression.


It then uses the regexec() function to match the regular expression against the text entered by the user. If a match is found, the program prints the matched IP address using the printf() function.

if no match is found, the program prints “No IP address found in text”.

Finally, the program frees the regular expression object using the regfree() function before exiting

Learn C-Sharp ↗

C-sharp covers every topic to learn about C-Sharp thoroughly.

Learn C Programming ↗

C-Programming covers every topic to learn about C-Sharp thoroughly.

Learn C++ Programming↗

C++ covers every topic to learn about C-Sharp thoroughly.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

C# Program to Delete Element at Desired position From Array

The Program in C# Program to Delete Element at Desired position From Array is given below: Output: Hello Codeauri Family, enter the number of elements in the array:4Enter…

Leave a Reply

Your email address will not be published. Required fields are marked *

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X