1#include <stdio.h>
2#include <string.h>
3int main()
4{
5 char a[20]="Program";
6 char b[20]={'P','r','o','g','r','a','m','\0'};
7
8 // using the %zu format specifier to print size_t
9 printf("Length of string a = %zu \n",strlen(a));
10 printf("Length of string b = %zu \n",strlen(b));
11
12 return 0;
13}
1#include <stdio.h>
2// function for string length
3int string_length(char str[])
4{
5 int i;
6 // you can use while loop
7 for( i = 0; str[i] != '\0';i++);
8 return i;
9}
10int main()
11{
12 // declaring character array
13 char your_name[20]; // you can change the maximum size of string
14 printf("Type you full Name : ");
15 // taking input from user
16 gets(your_name);
17 // using string length function
18 int length = string_length(your_name);
19 // printing string length
20 printf("\nLength of your name (%s) is : %d \n\n",your_name,length);
21
22}
23
1// include all the libraries used in the program.
2#include <stdio.h>
3#include <string.h>
4
5// Calculate Length of String Using strlen() Function
6int main()
7{
8 char a[100]; int length;
9 printf("Enter a string to calculate its length\n"); gets(a);
10 length = strlen(a);
11 printf("Length of the string = %d\n", length);
12 return 0;
13}
14
15// Calculate Length of String without Using strlen() Function
16int main()
17{
18 char s[] = "Programming is fun";
19 int i;
20
21 for (i = 0; s[i] != '\0'; ++i);
22
23 printf("Length of the string: %d", i);
24 return 0;
25}
1/**
2* A PROGRAM THAT RETURNS THE LENGTH OF A STRING FROM THE USER
3*/
4
5#include <stdio.h>
6
7/**
8* _strlen - takes a string and returns its length
9* @i: counter variable
10* @*s: the string entered by the user from the terminal
11* Return: Length of the string = i
12*/
13int _strlen(char *s)
14{
15 int i;
16
17 for(i = 0; s[i];)
18 i++;
19
20 return(i);
21}
22
23/**
24* main - start of this program
25* @str: string entered by user
26* Return: 0 when runs successfully
27*/
28int main()
29{
30 char str[100];
31
32 printf("Enter your string\n");
33 scanf("%s",str);
34
35 printf("%s is %i characters\n", str, _strlen(str));
36
37 return 0;
38}
1#include <stdio.h>
2#include <string.h>
3int main()
4{
5 char tab[1000];
6 int d = 0;
7
8 printf("enter the string :");
9 gets(tab);
10
11 while (tab[d] != '\0'){
12 d++;
13 }
14 for (int i=0;i<1000;i++){
15 if(tab[i]==' '){
16 d--;
17 }
18 }
19
20 printf(" the length of the string: %d\n", d);
21
22 return 0;
23}