C Online Compiler
Example: String Length with For Loop in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// String Length with For Loop #include <stdio.h> int main() { char str[] = "Hello World"; // The string to measure int length = 0; // Variable to store the length // Step 1: Iterate through the string using a for loop // The loop continues as long as the current character is not the null terminator '\0' for (int i = 0; str[i] != '\0'; i++) { // Step 2: Increment the length counter for each character found length++; } // Step 3: Print the calculated length printf("The string is: \"%s\"\n", str); printf("Length of the string (for loop): %d\n", length); return 0; }
Output
Clear
ADVERTISEMENTS