Ejemplo 1: strcmp c
// use: strcmp(string1, string2);
string a = "words";
string b = "words";
if (strcmp(a, b) == 0)
{
printf("a and b match");
// strcmp returns 0 if both strings match
}
else
{
printf("a and b don't match");
// strcmp returns anything else if the strings dont match
}
Ejemplo 2: biblioteca strcmp c
#include <string.h>
Ejemplo 3: strcmp en c
#include <stdio.h>
#include <string.h>
int main()
{
char str1[] = "abcd", str2[] = "abCd", str3[] = "abcd";
int result;
// comparing strings str1 and str2
result = strcmp(str1, str2);
printf("strcmp(str1, str2) = %dn", result);
// comparing strings str1 and str3
result = strcmp(str1, str3);
printf("strcmp(str1, str3) = %dn", result);
return 0;
}
Ejemplo 4: comparar dos caracteres c
strcmp(char, char);
Ejemplo 5: c strcmp
// strCmp implementation
// string1 < string2 => return a negative integer
// string1 > string2 => return a positive integer
// string1 = string2 => return 0
int strCmp(const char* s1, const char* s2) {
while(*s1 && (*s1 == *s2)) {
s1++;
s2++;
}
return *s1 - *s2;
}
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)