Programming with C

⌘K
  1. Home
  2. Docs
  3. Programming with C
  4. Arrays and Strings
  5. Strings
  6. String Length

String Length

strlen()

Find length of string

temp_variable = strlen(string_name);

Program: compute string Length using string function

#include<stdio.h>
#include<string.h>
int main()
{
	char s[100];
	int length;
	puts("Enter string");
	gets(s);
	length = strlen(s);
	printf("Length = %d",length);
	return 0;
}

Output

Enter string
computer
Length = 8

Program: Compute String Length without using library function

#include<stdio.h>
int main()
{
	char s[100];
	int i;
	puts("Enter string");
	gets(s);
	while(s[i] != '\0')
	{
		i++;
	}
	printf("Length = %d",i);
	return 0;
}

Output

Enter string
computer
Length = 8

Views: 1

How can we help?

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments