Alcune funzioni della stringa del terreno comunale
La biblioteca standard di string.h contiene molte funzioni utili per maneggiare le stringhe. Alcune delle funzioni più utili sono state exampled qui.
Strlen la funzione
Strlen la funzione è usato determinare la lunghezza di una stringa. Impariamo l'uso di strlen con l'esempio:
#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 l'esecuzione del programma sarà come segue:
Enter your name: Tarun Subhash Tyagi
Your name has 19 characters
Enter your name: Preeti Tarun
Your name has 12 characters |
La funzione strcpy
La funzione strcpy è usata per copiare una stringa ad un altro. Impariamo l'uso di questa funzione con l'esempio:
#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 l'uscita del programma sarà come:
Enter first string: Tarun
Enter second string: Tyagi
first: Tarun, and second: Tyagi Before strcpy()
first: Tarun, and second: Tarun After strcpy() |
|