Program to Calculate length of String in C Language

Spread the love
Program-to-Calculate-length-of-String-in-C-Language

Program Description:

Write a Program to Calculate the Length of String in C programming language. We are going to look at different methods to calculate the length of the strings. They are

Example Input and Output:

Input:

Enter a string : Welcome

Output:

Length of the Given string 'Welcome' is : 7

Prerequisites:

It is recommended to have knowledge of C strings. Please go through the following articles to learn more about the strings.

Calculate Length of String without using the strlen function (Iterative Way):

We can calculate the length of the string by iterating over the elements of the string and counting the number of characters until we reach the end of the string. As we already know the strings are terminated with the NULL characters. So we need to count the number of characters in the string till we reach the terminating NULL character.

Here is a step-by-step explanation of the program.

Calculate String Length using Iterative Method Program Explanation:

  1. Start the program by declaring a string and let’s say it is as str with the size SIZE(i.e 100).
  2. Prompt the user to provide the string for str and read it using the gets function. (The gets function won’t check the boundaries, so use it with caution).
  3. Initialize a variable named strLength with Zero(0). This variable contains the length of the given string ( str).
  4. Create a For loop to iterate over the string. Start the loop from index zero and go till we reach the terminating NULL character \0.
    • At each iteration, Increment the strLength variable – strLength++;
  5. Print the result on the console.

📢 This Program is part of the C String Practice Programs series

Program (Iterative Method ):

Program Output:

Compile and Run the program using your favorite compiler. We are using GCC compiler in this example.

The input string Welcome has 7 characters

calculate-string-length-using-iterative-method

Let’s try a few more examples

The program is generating the excepted results.

Calculate Length of String using User-defined function(without strlen) in C:

Let’s look at the second method, Calculating the string Length using a custom user-defined function ( without strlen library function).

Let’s rewrite the above program and create a function named mystrlen. The mystrlen function takes a character pointer str (which points to the input string) and returns the length of the string ( str)

Here is the prototype of the custom string length function

int mystrlen(char *str)

The mystrlen function also uses a for loop to iterate over the string( str) and calculates the length of the string and returns the results back to the caller.

Here is the string length program with user-defined string length ( mystrlen) function.

Program Output:

Let’s compile and Run the program

calculate-string-length-using-user-defined-function-program-output

The program is properly calculating the string length.

Program to Calculate Length of the string using strlen function in C:

The standard C Library provides the in-built function to calculate the length of the string, which is strlen() function. The strlen() function is declared in the string.h header file.

Let’s look at a program to calculate the string length using the strlen library function.

Program Output:

Compile and Run the program

calculate-string-length-using-strlen-function

📢We have covered the strlen() library function in detail in the following article

Call the strlen() function with the input string and strlen() function calculates and returns the string length.

int strLength = strlen(str);

📢 It is recommended to use the strlen library function to calculate the string length. It is generally faster and more efficient than our custom implementations. So it is better to use the library functions.

Related Programs:

Venkat

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

1 Response

  1. […] C Program to Calculate Length of the String […]

Leave a Reply