Quelques fonctions de corde de terrain communal
La bibliothèque standard de string.h contient beaucoup de fonctions utiles pour manoeuvrer des cordes. Certaines des fonctions les plus utiles ont été exampled ici.
Strlen la fonction
Strlen la fonction est employé pour déterminer la longueur d'une corde. Apprenons l'utilisation de strlen avec l'exemple :
#include <stdio.h>
#include <string.h>
int main()
{
char name[80];
int length;
printf("Enter your name: ");
gets(name);
length = strlen(name);
printf("Your name has %d characters\n", length);
return 0;
}
Et l'exécution du programme sera comme suit :
Enter your name: Tarun Subhash Tyagi
Your name has 19 characters
Enter your name: Preeti Tarun
Your name has 12 characters |
La fonction strcpy
La fonction strcpy est employée pour copier une corde à l'autre. Apprenons l'utilisation de cette fonction avec l'exemple :
#include <stdio.h>
#include <string.h>
int main()
{
char first[80];
char second[80];
printf("Enter first string: ");
gets(first);
printf("Enter second string: ");
gets(second);
printf("first: %s, and second: %s Before strcpy()\n "
, first, second);
strcpy(second, first);
printf("first: %s, and second: %s After strcpy()\n",
first, second);
return 0;
}
et le rendement du programme sera comme :
Enter first string: Tarun
Enter second string: Tyagi
first: Tarun, and second: Tyagi Before strcpy()
first: Tarun, and second: Tarun After strcpy() |
|