C Program
#include <stdio.h> int main() { char str[100]; int i, length = 0; printf("Enter a string: "); scanf("%s", str); for (i = 0; str[i] != '\0'; i++) { length++; } printf("Length of string = %d\n", length); return 0; }
C Output
Input: HelloWorld Output: Length of string = 10
C++ Program
#include <iostream> using namespace std; int main() { char str[100]; int length = 0; cout << "Enter a string: "; cin >> str; for (int i = 0; str[i] != '\0'; i++) { length++; } cout << "Length of string = " << length << endl; return 0; }
C++ Output
Input: OpenAI Output: Length of string = 6
JAVA Program
import java.util.Scanner; public class StringLengthManual { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter a string: "); String str = sc.next(); int length = 0; for (int i = 0; i < str.length(); i++) { length++; } System.out.println("Length of string = " + length); } }
JAVA Output
Input: Computer Output: Length of string = 8
Python Program
str1 = input("Enter a string: ") length = 0 for ch in str1: length += 1 print("Length of string =", length)
Python Output
Input: Python Output: Length of string = 6
Explanation
Example
If the user types "Hello", the program begins from the first character and increments one by one until it reaches the null character ('\\0' in C/C++ or end of string in Java/Python). "Hello" contains 5 characters, so the loop is executed 5 times and hence the length is 5.
Real-Life Analogy
Consider string length counting as trying to measure the number of beads in a necklace. You never know how many beads there are unless you count every single one until you've reached the end. The same is true for computers, which count characters individually until they reach the stopping place (the null terminator or end of the input).
Why It Matters
In actual programming, we usually want to know the length of a string. For instance, password validation involves determining whether the user has inserted no less than 8 characters. File names, messages, and web addresses are strings which tend to need to be length-checked quite often. Searching length by hand allows novices to grasp how strings are stored and handled internally by a computer.
Learning Insight
Most contemporary programming languages have in-built functions such as strlen() in C, .length() in Java, or len() in Python, but knowing how to count the characters yourself imparts the basic principle of string termination. It illustrates how strings are merely an array of characters, where length is calculated by walking until the end marker.
Use in Interviews and Projects
This is a common interview question to verify a candidate's knowledge of arrays, strings, and loops. Programmers are usually asked to solve the problem without using library functions by the interviewers. Although in actual projects we use built-in functions most of the time, this is used when we create custom parsers, compilers, or network protocols where raw character count is required.
SEO-Optimized Conclusion
Knowing how to manually compute the length of a string in C, C++, Java, and Python is an important milestone to achieving mastery in string manipulation for beginners. The idea not only solidifies your understanding of arrays and loops but also lays the groundwork for tackling more sophisticated issues such as substring manipulation, pattern matching, and memory-aware string manipulation. Whether for interviews, coding contests, or school exams, practicing string length without using built-in functions is a crucial skill that enhances logical reasoning and coding speed.
Social Plugin