Home » C programs » C String User Defined Functions

C program to calculate length of the string without using strlen()

In this program, we will calculate length of the string without using strlen() function, strlen() is a library function of string.h that returns length of the string.

Here, we will count the total number of characters available in a string and will not use strlen().

Logic

  • Read a string
  • Create a function and pass string within it
  • Take a counter as length (that will store the length of the string), initialize with 0
  • Run a loop until NULL character is not found
  • Increase the counter (length)
  • As NULL founds return the length

C example to calculate string length



#include <stdio.h>

/*function to return length of the string*/
int stringLength(char*);

int main()
{
	char str[100]={0};
	int length;

	printf("Enter any string: ");
	scanf("%s",str);
	
	/*call the function*/
	length=stringLength(str);
	
	printf("String length is : %d\n",length);

	return 0;
}

/*function definition...*/
int stringLength(char* txt)
{
	int i=0,count=0;
	
	while(txt[i++]!='\0'){
		count+=1;
	}
	
	return count;
}

Output

Enter any string: IncludeHelp 
String length is : 11





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.