Algumas funções da corda da terra comum
A biblioteca padrão de string.h contem muitas funções úteis para manipular cordas. Algumas das funções as mais úteis foram exampled aqui.
The strlen Function
Strlen a função é usado determinar o comprimento de uma corda. Deixar-nos aprender o uso de strlen com exemplo:
#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;
}
E a execução do programa será como segue:
Enter your name: Tarun Subhash Tyagi
Your name has 19 characters
Enter your name: Preeti Tarun
Your name has 12 characters |
The strcpy Function
A função strcpy é usada copí uma corda a outra. Deixar-nos aprender o uso desta função com exemplo:
#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;
}
e a saída do programa será como:
Enter first string: Tarun
Enter second string: Tyagi
first: Tarun, and second: Tyagi Before strcpy()
first: Tarun, and second: Tarun After strcpy() |
|