C Online Compiler
Example: String Length with While Loop and Pointer in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// String Length with While Loop and Pointer #include <stdio.h> int main() { char str[] = "C Programming"; // The string to measure char *ptr = str; // Pointer to the beginning of the string int length = 0; // Variable to store the length // Step 1: Iterate through the string using a while loop and pointer // The loop continues as long as the character pointed to is not the null terminator '\0' while (*ptr != '\0') { // Step 2: Increment the length counter length++; // Step 3: Move the pointer to the next character ptr++; } // Step 4: Print the calculated length printf("The string is: \"%s\"\n", str); printf("Length of the string (while loop with pointer): %d\n", length); return 0; }
Output
Clear
ADVERTISEMENTS