Comparing Strings in C program

In programming, ensuring secure password authentication is crucial for protecting sensitive data and maintaining system integrity. Let’s explore a simple C program that demonstrates password authentication using different types of loops: while, do-while, and for.

Program Description

The program prompts the user to enter a password. If the entered password matches a predefined correct password ("Cosmos@32" in this case), the program displays a welcome message and exits. If the password entered is incorrect, it notifies the user and prompts for the password again until the correct password is entered.

Code Explanation

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

int main(void) {
    char pass[20];

    // Using while loop
    /*
    while (1) {
        printf("Enter the password:\n");
        scanf("%s", pass);
        if (strcmp(pass, "Cosmos@32") == 0) {
            printf("Welcome\n");
            break;
        } else {
            printf("Incorrect password\n");
        }
    }
    */

    // Using do-while loop
    /*
    do {
        printf("Enter the password:\n");
        scanf("%s", pass);
        if (strcmp(pass, "Cosmos@32") == 0) {
            printf("Welcome\n");
            break;
        } else {
            printf("Incorrect password\n");
        }
    } while(1);
    */

    // Using for loop (infinite loop)
    for (;;) {
        printf("Enter the password:\n");
        scanf("%s", pass);
        if (strcmp(pass, "Cosmos@32") == 0) {
            printf("Welcome\n");
            break;
        } else {
            printf("Incorrect password\n");
        }
    }

    return 0;
}

How the Program Works

  1. Initialization: The program starts by declaring an array pass of type char to store the entered password.
  2. Password Validation Loop:
  • While Loop: The first section of code (commented out) uses a while loop. It continuously prompts the user to enter a password. If the entered password matches "Cosmos@32", it prints “Welcome” and exits the loop using break. Otherwise, it notifies the user of an incorrect password and repeats the process.
  • Do-While Loop: The second section (also commented out) demonstrates a do-while loop. This loop structure guarantees that the password prompt is displayed at least once, even if the correct password is entered initially. It continues to prompt until the correct password is entered.
  • For Loop: The final section utilizes a for loop, which in this case, is an infinite loop (for (;;)). It functions similarly to the while loop, continuously prompting the user until the correct password is entered.
  1. Password Comparison: Inside each loop, the program uses strcmp() from <string.h> to compare the user-input password (pass) with the correct password ("Cosmos@32"). If they match (strcmp() returns 0), it prints “Welcome” and breaks out of the loop. Otherwise, it informs the user that the password entered is incorrect.

Conclusion

This program showcases different loop structures (while, do-while, and for) in C programming for implementing secure password authentication. Understanding how loops can be used to repeatedly prompt for user input until a specific condition is met is essential for building robust and interactive applications. Practicing and mastering these concepts will strengthen your proficiency in C programming and help you develop more sophisticated software solutions.

Feel free to experiment with this program, modify the password, or extend its functionality to suit different authentication needs in your projects. Happy coding!

Leave a Comment

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

Scroll to Top