The strcmp Function
A função do strcmp é usada comparar junto duas cordas. O nome variável de uma disposição aponta ao endereço baixo dessa disposição. Conseqüentemente, se nós tentássemos comparar duas cordas usando o seguinte, nós estaríamos comparando dois endereços, que obviamente nunca seriam os mesmos como não é possível armazenar dois valores na mesma posição.
if (first == second) /* It can never be done to
compare strings */
O seguinte exemplo usa a função do strcmp comparar duas cordas:
#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;
}
E a execução do programa será como segue:
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 |
The strcat Function
A função do strcat é usada juntar uma corda a outra. Deixar-nos ver como? Com a ajuda do exemplo:
#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;
}
E a execução do programa será como segue:
Enter a string: Data
Enter another string: Recovery
The two strings joined together: DataRecovery |
|