La fonction de strcmp
La fonction de strcmp est employée pour comparer deux cordes ensemble. Le nom variable d'une rangée se dirige à l'adresse de base de cette rangée. Par conséquent, si nous essayons de comparer deux cordes en utilisant le suivant, nous comparerions les deux adresses, qui ne seraient évidemment jamais identiques qu'il n'est pas possible de stocker deux valeurs dans le même endroit.
if (first == second) /* Il peut ne jamais être fait pour comparer des cordes */
L'exemple suivant emploie la fonction de strcmp pour comparer deux cordes :
#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;
}
Et l'exécution du programme sera comme suit :
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 fonction de strcat
La fonction de strcat est employée pour joindre une corde à l'autre. Voyons comment ? Avec l'aide de l'exemple :
#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;
}
Et l'exécution du programme sera comme suit :
Enter a string: Data
Enter another string: Recovery
The two strings joined together: DataRecovery |
|