strlen() length of a string

#include <stdio.h>
#include <string.h>
int main(void){
char str[100]="plus2net"; // input string
int str_length; // integer variable

str_length=strlen(str); // length of the string
printf("Length of the string  = %d ", str_length);  // final output
return 0;
}
The output of above code is here
Length of the string = 8
We can use string function strlen() to get number of chars present in a string.
strlen(string);

Example Using one input string

#include <stdio.h>
#include <string.h>
int main(void){
char str[100]; // input string
int str_length; // integer variable
	printf("Enter your string  ");
	scanf("%s",str);
	str_length=strlen(str);
	printf(" Length of the input string :  %d", str_length); // printing outputs
return 0;
}
For input plus2net.com , output is 12

Length of the string without using strlen() function

C String We will print each character of the string by starting from the beginning ( 0th position ). We know each string is terminated by a null character ( '\0' ) so we will use while loop and check condition if null character is not reached.
#include<stdio.h>
 int main()
{
 char str[12]="plus2net";
 int i=0;
 while (str[i] !='\0'){
// print value of i and value of string at that position. // 
   printf("i = %d : %c \n",i,str[i]); // Output 
   i=i+1; // increment of i by 1 
 }
return 0;
}
Output is here
i = 0 : p
i = 1 : l
i = 2 : u
i = 3 : s 
i = 4 : 2
i = 5 : n
i = 6 : e
i = 7 : t
We know the length of the string is equal to value of our variable i. Here is the code to display the length.
#include<stdio.h>
 int main()
{
 char str[12]="plus2net";
 int i=0;
 while (str[i] !='\0'){
// print value of i and value of string at that position. //
   printf("i = %d : %c \n",i,str[i]); // Output
   i=i+1; // increment of i by 1
 }
 printf("Length of the string = %d ",i); // Output as length of the string
return 0;
}

Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here




    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer