La funzione dello strcmp
La funzione dello strcmp è usata per confrontare insieme due stringhe. Il nome variabile di un allineamento indica all'indirizzo basso di quell'allineamento. Di conseguenza, se proviamo a confrontare due stringhe usando quanto segue, stavamo confrontando i due indirizzi, che non sarebbero ovviamente mai gli stessi come non fosse possibile memorizzare due valori nella stessa posizione.
if (first == second) /* It can never be done to
compare strings */
Il seguente esempio usa la funzione dello strcmp per confrontare due stringhe:
#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 l'esecuzione del programma sarà come 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 |
La funzione dello strcat
La funzione dello strcat è usata per unire una stringa ad un altro. Vediamo come? Con l'aiuto dell'esempio:
#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 l'esecuzione del programma sarà come segue:
Enter a string: Data
Enter another string: Recovery
The two strings joined together: DataRecovery |
|