Length of String without using Library Function in C Programming

Introduction

In this post, I am going to find the length of a string without using the library function in c programming.

Also Read: Switch Case in C Program to Calculate Area of Circle and Triangle

In the c programming, there is a standard library function i.e. strlen() which finds the length of the string. For better understanding, I will write three programs. One is with standard library function and the other two programs without using library functions.

Before writing these three programs, let us see the expected output first.

Expected Output

length of string without using library function in c programming output

All three programs will have the same output. Now, let us see each and every program.

Also Read: C Program to Print Multiples of 5 using do-while loop

Find Length of a String Using Library Function

#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int main()
{
    char str[100];
    int len;
    printf("Enter any string: ");
    scanf("%s",str);
    len=strlen(str);
    printf("The length of string %s is %d",str,len);
    return 0;
}

In the above program, I have used the strlen function. It counts the number of characters in a string.

Now, let us see the other two programs without using the library function.

Length of String without using Library Function

#include <stdio.h>
#include <stdlib.h>
int main()
{
    char str[100];
    int len=0,i=0;
    printf("Enter any string: ");
    scanf("%s",str);
    while(str[i]!='\0')
    {
        len++;
        i++;
    }
    printf("The length of string %s is %d",str,len);
    return 0;
}

You can easily spot the difference between the above two programs. In the second program, we are simply running a while loop until we get a null character i.e. ‘\0’. This will tell us that string is over.

Now, in the next program, we are finding the length of the string using pointers.

Length of String without using Pointer in C Programming

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int count=0;
    char s[50],*ptr;
    printf("Enter a String: ");
    gets(s);
    ptr=s;
    while(*ptr!='\0')
    {
        count++;
        ptr++;
    }
    printf("Length of string %s is %d",s,count);
    return 0;
}

In the above program, we are simply reading a string from the user. Then we are assigning the address of the first character to the pointer variable.

We know, pointer stores the address of the normal variable. The *ptr means the value at the address which is stored in the pointer variable. In the while loop, we are incrementing the value of ptr. In other words, we are incrementing the address.

As I said above, the output will remain the same.

I hope you have understood the above programs. If you still have any doubts regarding the above programs, kindly let me know.

Thank you.

Some Important C Programs

  1. Program in C to Find Longest Line in a File
  2. Palindrome in C using Pointers
  3. Insert and Delete element in Array in C using switch case
  4. C Program to Add Alternate Elements of a 2D Array
  5. Arrays in C for Complete Beginners
  6. C Program to Find Area of a Circle using Preprocessor
  7. Program in C to Remove White Spaces and Comments from a File
  8. C Program to Print Numbers Except Multiples of n
  9. Reverse a Number using getchar and putchar function in c
  10. The while loop in C Programming

FAQs

Is strlen a library function?

Yes. This function is used in c programming. This function finds the length of a string i.e. total number of characters in a string.

What is the length of a string?

The length of a string means the number of characters in a string.

What is the size of the string variable in C?

There is no fixed size of the string in c programming. Actually, there is no string variable in c programming.

How do you use the strlen function?

For using this function, you must include the header file i.e. string.h
Suppose the string is “Vijay”, then we need to write
len = strlen(“Vijay”). The value of len will become 5.

What does strlen () return in C?

It returns an integer value. That means the return type of strlen() is an integer.

Leave a Comment