#include <stdio.h>
#include <stdlib.h>
#include <string.h> // Include for string functions like strcpy, strcat, and strcmp

// Function declarations
void usingSprintf();        // Function to demonstrate sprintf usage
void usingScanfAndStrcmp(); // Function to demonstrate scanf and strcmp usage
void usingSscanf();         // Function to demonstrate sscanf usage

int main()
{
    char example1[] = "Hi!\0XMUT";
    char example2[] = "Hi!XMUT";
    char example3[] = "Hi!";
    int length1 = strlen(example1);
    int length2 = strlen(example2);
    int size = sizeof(example1);
    printf("Size of example1 is %d\n",size);
    printf("Length of example1 is %d\n",length1);
    printf("Length of example2 is %d\n",length2);
    printf("Print example1: %s\n",example1);
    if(strcmp(example1,example3)==0){
        printf("They are the same!\n");
    }

    // char arr[6];
    // scanf("%s",arr);
    // scanf("%[^\n]",arr);
    // printf("arr is %s\n",arr);

    // scanf("%c",&arr[1]);
    // printf("arr[1] is %c\n",arr[1]);
    // printf("arr is %s\n",arr);
    // return 0;


    char str[] = "Hello, World!";

    // Default printing without any formatting
    printf("Default:               [%s]\n", str);

    // Print with a minimum width of 20, right-justified
    printf("Minimum width 20:      [%20s]\n", str);

    // Print left-justified within a field of width 20
    printf("Left-justified width:  [%-20s]\n", str);

    // Print only the first 5 characters of the string
    printf("Maximum 5 chars:       [%.5s]\n", str);

    // Print with a minimum width of 20 and only the first 5 characters
    printf("Width 20, max 5 chars: [%20.5s]\n", str);

    // Left-justified, with a width of 20, and printing only the first 5 characters
    printf("Left-justified width 20, max 5 chars: [%-20.5s]\n", str);

    // Call to function that demonstrates the use of `scanf` and `strcmp`
    // usingScanfAndStrcmp();

    // Call to function that demonstrates the use of `sprintf`
    // usingSprintf();

    // Call to function that demonstrates the use of `sscanf`
    // usingSscanf();

    return 0;
}

/*
 * Function: usingScanfAndStrcmp
 * ------------------------------
 * Demonstrates how to use `scanf` to take input and `strcmp` to compare strings.
 * In this case, it prompts the user to enter a password and checks it against a
 * predefined correct password.
 */
void usingScanfAndStrcmp()
{
    char greeting[100] = "Hello, ";  // Base greeting message
    char password[20];               // User input for password
    char correct_password[] = "secret123"; // The correct password to compare against

    printf("Enter the password: ");
    scanf("%19s", password); // Read up to 19 characters (to prevent buffer overflow)

    // Compare the entered password with the correct password
    if (strcmp(password, correct_password) == 0)
    {
        printf("Access granted.\n");
    }
    else
    {
        printf("Access denied.\n");
    }
    
    // Print the greeting message
    printf("%s\n", greeting);
}

/*
 * Function: usingSprintf
 * ------------------------
 * Demonstrates how to use `sprintf` to create formatted strings.
 * This function takes input for the first name, last name, and age,
 * concatenates the first and last names into a full name, and then
 * uses `sprintf` to generate a greeting message.
 */
void usingSprintf()
{
    char firstName[50], lastName[50], fullName[100];
    int age;

    // Prompt user for their first name
    printf("Enter your first name: ");
    scanf("%49s", firstName); // Read up to 49 characters

    // Prompt user for their last name
    printf("Enter your last name: ");
    scanf("%49s", lastName);  // Read up to 49 characters

    // Prompt user for their age
    printf("Enter your age: ");
    scanf("%d", &age);        // Read an integer value

    // Concatenate first and last names into fullName
    strcpy(fullName, firstName);
    strcat(fullName, " ");    // Add a space between the names
    strcat(fullName, lastName);

    // Create a greeting message with `sprintf`
    char greeting[150];
    sprintf(greeting, "Hello, %s! You are %d years old.", fullName, age);

    // Output the generated greeting
    printf("%s\n", greeting);
}

/*
 * Function: usingSscanf
 * ----------------------
 * Demonstrates the usage of `sscanf`, which reads formatted input from a string.
 * In this case, it parses a string to extract a name and an age.
 */
void usingSscanf()
{
    char input[] = "John 25"; // Input string containing a name and age
    char name[10];            // Buffer to hold the extracted name
    int age;                  // Variable to hold the extracted age

    // Use `sscanf` to extract the name and age from the input string
    sscanf(input, "%s %d", name, &age);

    // Output the extracted values
    printf("Name: %s, Age: %d\n", name, age);
}