-9

I have two strings and want to compare them, but between two variables. Example: String a = "stackoverflow" and String b = "stacknotoverflow". I want to check if every char between the 5th position of String a (int = 5 till int =9) and 8th position of String b (int = 8 till int = 12) position of both strings is the same. Can anyone help me with this simple problem?

chilla77
  • 15
  • 3

2 Answers2

2

You can use the strncmp function to compare two strings up to a maximum number of characters. To start a comparison in the middle of a string, you would pass in the address of the array element to start the comparison at.

For example:

if (strncmp(&string1[4], &string2[4], 4) == 0) {
    printf("characters 5 - 8 are the same\n");
} else {
    printf("characters 5 - 8 are not the same\n");
}
dbush
  • 205,898
  • 23
  • 218
  • 273
-1

Assuming you're talking about C.

https://linux.die.net/man/3/strcmp

This function is used to compare strings in C.

int result = strcmp(str1, str2);
printf("Compare result: %d", result);
Someone
  • 21
  • 1