C String length

Syntax

C strlen function has the following syntax. size_t strlen(const char *str);

Example 1


#include <string.h>
#include <stdio.h>
// w ww  .  j a  v  a 2 s. co  m
int main()
{
    char line[100];
    printf("Enter a line: ");
    fgets(line, sizeof(line), stdin);

    printf("The length of the line is: %d\n", strlen(line));
    return (0);
}

Example 2


#include <stdio.h>
// ww  w  .  j a va 2 s.  co m
void main() {
  char str1[40] = "To be or not to be";
  char str2[40] = ",that is the question. ";
  int count = 0;                

  while (str1[count] != '\0')   
    count++; 
  printf("\nThe length of the string \"%s\" is %d characters.", str1, count);

  count = 0; 
  while (str2[count] != '\0')
    count++;
  printf("\nThe length of the string \"%s\" is %d characters.\n", str2, count);
}




















Home »
  C Language »
    Language Advanced »




File
Function definition
String
Pointer
Structure
Preprocessing