strlen
De cppreference.com
|
|
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
<metanoindex/>
<tbody> </tbody>| Definido no cabeçalho <string.h>
|
||
size_t strlen( char *str ); |
||
Retorna o tamanho do string sem contar o carácter '\0' que termina o string.
Original:
Returns the length of the given byte string.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Parâmetros
| str | - | ponteiro para um string terminado com o carácter '\0' (nulo).
Original: pointer to the null-terminated byte string to be examined The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
Valor de retorno
O comprimento do string terminado com o carácter nulo.
Original:
The length of the null-terminated string
str.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Exemplo
#include <stdio.h>
#include <string.h>
int main(void) {
char mensagem[] = "Eu amo programar em C";
size_t tamanho = strlen(mensagem);
printf("A mensagem \"%s\" tem %lu letras.\n", mensagem, tamanho);
// A função strlen() inicia a leitura no endereço de memória onde está o ponteiro
// e termina a leitura quando encontra o carácter nulo (NULL), que é representado
// por '\0'. Se nós movermos o ponteiro, que no momento aponta para o inicio da
// mensagem, três 'casas' para a direita, ele irá apontar para a letra 'a' da palavra
// amo. Quando pedirmos para a função strlen() retornar o tamanho do string, ela irá
// retornar a contagem da letra 'a' até o caracter '\0' (este não é contado).
printf("Agora a mensagem é: \"%s\" e tem %lu letras.\n",
mensagem+3, strlen(mensagem+3));
}
Saída:
A mensagem "Eu amo programar em C" tem 21 letras.
Agora a mensagem é: "amo programar em C" e tem 18 letras.
Veja também
C++ documentation for strlen
|