La función del strcmp
La función del strcmp se utiliza para comparar dos secuencias juntas. El nombre variable de un arsenal señala a la dirección baja de ese arsenal. Por lo tanto, si intentamos comparar dos secuencias usando el siguiente, estaríamos comparando los formatos de dos direcciones, que obviamente nunca serían iguales que no es posible almacenar dos valores en la misma localización.
if (first == second) /* It can never be done to
compare strings */
El ejemplo siguiente utiliza la función del strcmp para comparar dos secuencias:
#include <string.h>
int main()
{
char first[80], second[80];
int t;
for(t=1;t<=2;t++)
{
printf("\nEnter a string: ");
gets(first);
printf("Enter another string: ");
gets(second);
if (strcmp(first, second) == 0)
puts("The two strings are equal");
else
puts("The two strings are not equal");
}
return 0;
}
Y la ejecución del programa será como sigue:
Enter a string: Tarun
Enter another string: tarun
The two strings are not equal
Enter a string: Tarun
Enter another string: Tarun
The two strings are equal |
La función del strcat
La función del strcat se utiliza para ensamblar una secuencia a otra. ¿Veamos cómo? Con la ayuda de ejemplo:
#include <string.h>
int main()
{
char first[80], second[80];
printf("Enter a string: ");
gets(first);
printf("Enter another string: ");
gets(second);
strcat(first, second);
printf("The two strings joined together: %s\n",
first);
return 0;
}
Y la ejecución del programa será como sigue:
Enter a string: Data
Enter another string: Recovery
The two strings joined together: DataRecovery |
|