Design a function to compare two string in c

26 Sep 2022 Balmiki Mandal 0 C Programming

String Comparison Function in C: Implementation and Usage

In C, you can use the strcmp() function to compare two strings. This function takes two string arguments and returns an integer value indicating whether the strings are equal or not.

To design a function to compare two strings in C, we can use the following steps:

1. Declare a function prototype with the following signature:

C
int compare_strings(const char *string1, const char *string2);

The function takes two strings as input and returns an integer value. The integer value returned by the function indicates the relationship between the two strings:

  • A positive value indicates that the first string is greater than the second string.
  • A negative value indicates that the first string is less than the second string.
  • A zero value indicates that the two strings are equal.
  1. Implement the function body using a loop to compare the characters of the two strings one by one. The loop should stop when either the end of one of the strings is reached or a difference between the characters of the two strings is found.

  2. If the end of one of the strings is reached before a difference is found, the function should return a zero value, indicating that the two strings are equal.

  3. If a difference between the characters of the two strings is found, the function should return a positive or negative value, depending on which string has the greater ASCII value for the character at that position.

Example implementation of the compare_strings() function:

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

int compare_strings(const char *string1, const char *string2) {
  while (*string1 != '\0' && *string2 != '\0') {
    if (*string1 > *string2) {
      return 1;
    } else if (*string1 < *string2) {
      return -1;
    }

    string1++;
    string2++;
  }

  if (*string1 == '\0' && *string2 == '\0') {
    return 0;
  } else if (*string1 == '\0') {
    return -1;
  } else {
    return 1;
  }
}

Example of how to use the compare_strings() function:

C Programming
#include <stdio.h>
#include <stdlib.h>

int main() {
  char string1[] = "Hello, world!";
  char string2[] = "This is a test string.";

  int result = compare_strings(string1, string2);

  if (result > 0) {
    printf("%s is greater than %s.\n", string1, string2);
  } else if (result < 0) {
    printf("%s is less than %s.\n", string1, string2);
  } else {
    printf("%s is equal to %s.\n", string1, string2);
  }

  return 0;
}

Output:

 

Hello, world! is less than This is a test string.

Further Reading:


Implementation of strncat() in C

add source string at the end of destination string

comparing two string using strcmp and strncmp

Design a function to copy to copy string into destination string using strcpy and strncpy

how to implement implementation of strncpy in c

Design a function to calculate the length of the string using strlen

pre defined string based function

Design a function using my_strcpy and using my_strchr we need to select given char in given string

 

Enroll Now:

C-Programming From Scratch to Advanced 2023-2024] "Start Supercharging Your Productivity!"

Contact Us:

  • For any inquiries, please email us at [[email protected]].
  • Follow us on insta  [ electro4u_offical_ ] for updates and tips.

Note: If you encounter any issues or specific errors when running this program, please let me know and I'll be happy to help debug them!

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.