Dado um string str . A tarefa é encontrar o comprimento da string.

Exemplos :

Input: str = "Geeks"
Output: Length of Str is : 4

Input: str = "GeeksforGeeks"
Output: Length of Str is : 13

No programa abaixo, para encontrar o comprimento da string str , primeiro a string é tomada como entrada do usuário usando scanf in Stre, em seguida, o comprimento de Str é calculado usando cicloe usando o strlen()método.

Abaixo está o programa C para encontrar o comprimento da string.

Exemplo 1: Uso de loop para calcular o comprimento da string.

// C program to find the length of string
#include <stdio.h>
#include <string.h>
  
int main()
{
    char Str[1000];
    int i;
  
    printf("Enter the String: ");
    scanf("%s", Str);
  
    for (i = 0; Str[i] != '\0'; ++i);
  
    printf("Length of Str is %d", i);
  
    return 0;
}
Saída:
Digite a string: Geeks
Comprimento de Str é 5

Exemplo 2: Usando strlen() para encontrar o comprimento da string.

// C program to find the length of 
// string using strlen function
#include <stdio.h>
#include <string.h>
  
int main()
{
    char Str[1000];
    int i;
  
    printf("Enter the String: ");
    scanf("%s", Str);
  
    printf("Length of Str is %ld", strlen(Str));
  
    return 0;
}
Saída:
Digite a string: Geeks
Comprimento de Str é 5