C program to find length of string using pointers


Discover how to find the length of a string in C programming using pointers efficiently. This article provides a step-by-step guide and complete code example, empowering programmers to enhance their understanding of string manipulation and pointer usage in C.

In C programming, strings are sequences of characters terminated by a null character ('\0'). Determining the length of a string is a common operation, crucial for various string manipulation tasks. Leveraging pointers in C, we can efficiently compute the length of a string, offering a dynamic and optimized solution.

In this article, we'll delve into how to find the length of a string using pointers in C. Understanding this concept is essential for mastering string manipulation and memory management in the C programming language.

Understanding Pointers and Strings in C

Before we dive into finding the length of strings using pointers, let's grasp the basics of pointers and strings in C:

  • Pointers: Pointers in C are variables that store memory addresses. They enable direct access to memory locations, allowing for efficient memory management and manipulation of data.
  • Strings: Strings in C are arrays of characters terminated by a null character ('\0'). They represent text data and are widely used in various programming tasks.

Finding String Length using Pointers

To find the length of a string using pointers in C, we'll iterate through the characters of the string using a pointer until we encounter the null character ('\0'). Here's a step-by-step guide to finding string length using pointers:

  1. Declare String and Pointer: Define the string and take it as input and a pointer to traverse its characters.

    char str[100];
    char *ptr = str;
    
    printf("Enter string: ");
    gets(str);
    
  2. Iterate through Characters using Pointers: Iterate through the characters of the string using the pointer until the null character ('\0') is encountered.

    int length = 0;
    while (*ptr != '\0') {
        length++;
        ptr++;
    }
    
  3. Display String Length: Output the length of the string once the null character is encountered.

    printf("Length of the given string '%s': %d\n", str, length);
    

Write a C program to find length of string using pointers

Putting it all together, here's the complete C program to find the length of a string using pointers:

#include <stdio.h>

int main() {
    char str[100];
    char *ptr = str;

    printf("Enter string: ");
    gets(str);

    // Find string length using pointers
    int length = 0;
    while (*ptr != '\0') {
        length++;
        ptr++;
    }

    // Display string length
    printf("Length of the given string '%s': %d\n", str, length);

    return 0;
}

Output

Enter string: procoding
Length of the given string 'procoding': 9

Of course we can also use predefined library function strlen() to find length of string. strlen() is a string library function defined in string.h header file. It returns length of the string.

You can read more about this here.


Recommended Posts