Introduction
In C programming, pointers can be used to traverse a string and calculate its length. This guide will show you how to write a C program to find the length of a string using pointers.
Example:
- Input:
"Hello, World!"
- Output:
13
(the number of characters in the string, excluding the null terminator)
Problem Statement
Create a C program that:
- Takes a string as input from the user.
- Uses a pointer to traverse the string and count its characters.
- Displays the length of the string.
Solution Steps
- Include the Standard Input-Output Library: Use
#include <stdio.h>
for standard input-output functions. - Declare a String and a Pointer Variable: Declare a character array to store the string and a pointer to traverse the string.
- Input the String: Use
gets
orfgets
to take input from the user. - Calculate the Length Using the Pointer: Traverse the string using the pointer and count the number of characters until the null terminator (
'\0'
) is encountered. - Display the Length: Use
printf
to display the length of the string.
C Program to Find the Length of a String Using Pointers
#include <stdio.h> int main() { // Step 2: Declare a string and a pointer variable char str[100]; char *ptr; int length = 0; // Step 3: Prompt the user to enter the string printf("Enter a string: "); gets(str); // Use fgets(str, 100, stdin) if gets is deprecated // Step 4: Calculate the length using the pointer ptr = str; // Initialize pointer to the start of the string while (*ptr != '\0') { length++; ptr++; // Move the pointer to the next character } // Step 5: Display the length of the string printf("The length of the string is: %d\n", length); return 0; // Return 0 to indicate successful execution }
Explanation
Step 2: Declare a String and a Pointer Variable
- The character array
str
is declared to store the string entered by the user. - The pointer
ptr
is declared to traverse the string. - The variable
length
is initialized to0
and will be used to count the number of characters in the string.
Step 3: Input the String
- The program prompts the user to enter a string using
gets
. Note thatgets
may be unsafe; for safer input, you can usefgets(str, 100, stdin)
.
Step 4: Calculate the Length Using the Pointer
- The pointer
ptr
is initialized to point to the first character of the string (ptr = str
). - A
while
loop is used to traverse the string. The loop continues until the null terminator ('\0'
) is encountered. - During each iteration, the
length
variable is incremented, and the pointer is moved to the next character.
Step 5: Display the Length
- The program uses
printf
to display the length of the string.
Return 0
- The
return 0;
statement indicates that the program executed successfully.
Output Example
Example Output:
Enter a string: Hello, World! The length of the string is: 13
Conclusion
This C program demonstrates how to find the length of a string using pointers. It covers basic concepts such as pointer initialization, pointer traversal, and string manipulation. This example is useful for beginners learning how to work with strings and pointers in C programming.