Reverse Only Alphabets in C, C++, Java & Python – Code with Explanation & Examples in Short and Simple

   

C Program

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

int main() {
    char email[100];
    printf("Enter email: ");
    scanf("%s", email);

    regex_t regex;
    int result;

    // Simple regex for email validation
    result = regcomp(&regex, "^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$", REG_EXTENDED);
    if (result) {
        printf("Could not compile regex\n");
        return 1;
    }

    result = regexec(&regex, email, 0, NULL, 0);
    if (!result)
        printf("Valid Email\n");
    else
        printf("Invalid Email\n");

    regfree(&regex);
    return 0;
}

C Output

Input:  
[email protected] 

Output:  
Valid Email


C++ Program

#include <iostream>
#include <regex>
using namespace std;

int main() {
    string email;
    cout << "Enter email: ";
    cin >> email;

    regex pattern("^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$");

    if (regex_match(email, pattern))
        cout << "Valid Email" << endl;
    else
        cout << "Invalid Email" << endl;

    return 0;
}

C++ Output

Input:  
[email protected]  

Output:  
Valid Email


JAVA Program

import java.util.Scanner;
import java.util.regex.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter email: ");
        String email = sc.next();

        String regex = "^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(email);

        if (matcher.matches())
            System.out.println("Valid Email");
        else
            System.out.println("Invalid Email");

        sc.close();
    }
}

JAVA Output

Input:  
[email protected] 

Output:  
Valid Email


Python Program

import re

email = input("Enter email: ")

pattern = r'^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$'

if re.match(pattern, email):
    print("Valid Email")
else:
    print("Invalid Email")

Python Output

Input:  
wrong-email@com 

Output:  
Invalid Email


Explanation
Example
When you enter "[email protected]", the program verifies if it is in the standard email format:

A username (student123) composed of alphabets, numbers, or special characters such as ., _, %, +, -.

An @ symbol between the user and domain.

A correct domain name (college.edu) ending with a top-level domain such as .com, .edu, .org, .net, etc.

If all is correct, it is Valid Email; otherwise, it's Invalid Email.

Real-Life Analogy
Consider an email address as a good postal address. As a letter requires a house number, street, city, and postal code in a specific format to be delivered, an email similarly requires a specific format. If the address is corrupted, then the postman won't be able to deliver it. Likewise, mail servers discard corrupted email IDs.

Why It Matters
Verifying if an email exists is among the most frequent real-world programming tasks. Sites, apps, and sign-up systems all ask for a valid email address from users. Without validation, the system can accumulate bogus or invalid data and cause security vulnerabilities, spam, or communication breakdowns.

Learning Insights
This course educates us about how regular expressions (regex) are used across various programming languages. Regex is an extremely useful tool that facilitates pattern matching in strings. Knowing how to construct and test regex is crucial for input checking, parsing data, and dealing with real-world text-related issues. For novice learners, this course is a crash course on regex ideas, but for experienced learners, it's a door to strong validation methods.

Interview and Real-World Relevance
The questions asked in interviews are usually about regex fundamentals, email validation, and string pattern verification. The interviewers wish to determine whether you know how to impose data validation. In actual projects, any signup form, login system, or API needs user input validation to avoid mistakes or malicious attacks. This code demonstrates both your regex expertise and your capability to write effective solutions.

SEO-Optimized Closing
Validating an email pattern is an intrinsic programming task that is extensively employed in web application development, database management, and user authentication systems. New developers learning C, C++, Java, and Python can practice email validation programs as a means to consolidate string manipulation and regex skills. With this explicit explanation and ready source code in several programming languages, coding interview students, online exam candidates, or actual project students can readily learn to create a program to validate valid email patterns.