The strlen function is used to calculate total length of the given string.
STRLEN() FUNCTION EXAMPLE
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
clrscr();
char s[60];
int L;
puts("Enter any sentence: ");
gets(s);
L=strlen(s);
printf("Total length is : %d", L);
getch();
}
----------------------------
OUTPUT
----------------------------
Enter any sentence:
My name is rakesh
Total length is: 17
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
clrscr();
char s[60];
int L;
puts("Enter any sentence: ");
gets(s);
L=strlen(s);
printf("Total length is : %d", L);
getch();
}
----------------------------
OUTPUT
----------------------------
Enter any sentence:
My name is rakesh
Total length is: 17
C PROGRAM TO CALCULATE STRING LENGTH WITHOUT STRLEN() FUNCTION
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
char s[60];
int i, count=0;
puts("Enter any sentence: ");
gets(s);
----------------------------
OUTPUT
----------------------------
Enter any sentence:
My name is rakesh
Total length is: 17
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
char s[60];
int i, count=0;
puts("Enter any sentence: ");
gets(s);
// Loop to count total length of the given string
for(i=0; s[i] != '\0'; i++)
{
count++;
}
printf("Total length is : %d", count);
getch();
}
OUTPUT
----------------------------
Enter any sentence:
My name is rakesh
Total length is: 17
No comments:
Post a Comment