Wednesday, April 30, 2025

C Program - strcmp() Function - Compares Two Strings Character by Character

This is a C program that demonstrates how to compare two strings using the strcmp() function. 

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

int main()
{

    char left[] = "aaa";
    char right[] = "aaa";

    int compare_strings;

    compare_strings = strcmp(left, right);

    //printf("%d", compare_strings);

    if (compare_strings == 0)
    {
        printf("Same");
    }
    else if (compare_strings < 0)
    {
        printf("Left Positionaly Smaller");
    }
    else if (compare_strings > 0)
    {
        printf("Left Positionaly Bigger");
    }

    return 0;

}

Result: 

Same
Process returned 0 (0x0)   execution time : 0.035 s
Press any key to continue.

Here's a breakdown of the code block by block: 

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

These are header files that are included in the program to provide definitions for functions used later in the code. stdio.h is included for standard input/output functions, while string.h is included for string manipulation functions. 

int main()
{

This is the main function of the program, where execution begins. The function returns an integer value (in this case, 0) to indicate the status of the program when it exits. 

    char left[] = "aaa";
    char right[] = "aaa";

    int compare_strings;

    compare_strings = strcmp(left, right);

This declares two character arrays, left and right, and initializes them both with the same string "aaa". It also declares an integer variable compare_strings.

The strcmp() function is used to compare the two strings. The function returns an integer value that indicates the lexicographic relation between the two strings. The value 0 indicates that the strings are equal, a negative value indicates that left is lexicographically smaller than right, and a positive value indicates that left is lexicographically larger than right

    if (compare_strings == 0)
    {
        printf("Same");
    }
    else if (compare_strings < 0)
    {
        printf("Left Positionaly Smaller");
    }
    else if (compare_strings > 0)
    {
        printf("Left Positionaly Bigger");
    }

This uses a series of if statements to check the value of compare_strings and print out a message based on the result.

If compare_strings is 0, it means that the two strings are equal, so the program prints "Same".

If compare_strings is negative, it means that left is lexicographically smaller than right, so the program prints "Left Positionaly Smaller".

If compare_strings is positive, it means that left is lexicographically larger than right, so the program prints "Left Positionaly Bigger"

    return 0;
}

This statement ends the main() function and returns the value 0 to indicate successful program execution.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...