Kodekraftt
/Blog
/Count The Length Of A String In Java Without Using Length Definition Examples And Program
Join Early AccessContact UsPrivacy Policy
Java BasicsOOPsDSA with JavaQuizzesInterview Preparation

© 2026 KodeKraftt. All rights reserved.

Build smarter. Learn more. Innovate better.

Count the Length of a String in Java Without Using length() — Definition, Examples and Program

Learn how to find the length of a string in Java without using the built-in length() method. This blog explains the logic with examples, a Java program, and practice challenges.

Count the Length of a String in Java Without Using length()

Finding the length of a string without using the built-in length() method is a popular beginner and interview question in Java.

This problem helps you understand:

  • How strings are traversed internally
  • Character-by-character processing
  • Basic loop logic

How the Logic Works

To find the length of a string manually:

  1. Start a counter variable.
  2. Traverse the string character by character.
  3. Increment the counter for each character.
  4. Stop when all characters are processed.

Example 1

Input: "Java"

  • J → count = 1
  • a → count = 2
  • v → count = 3
  • a → count = 4

Length: 4

Example 2

Input: "Hello World"

  • Includes space as a character
  • Total characters counted = 11

Length: 11

Java Program to Count String Length Without length()


import java.util.Scanner;

public class StringLengthWithoutMethod {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a string: ");
        String str = sc.nextLine();

        int count = 0;

        for (char ch : str.toCharArray()) {
            count++;
        }

        System.out.println("Length of the string: " + count);
    }
}

Sample Output


Enter a string: Programming
Length of the string: 11

Important Notes

  • Spaces are counted as characters.
  • Special characters are also counted.
  • This approach does not use length().

Practice Challenges

  1. Count string length without converting it to a character array.
  2. Count length using a while loop instead of a for loop.
  3. Find the length of a string excluding spaces.

This program builds a strong foundation for understanding how strings work internally in Java.

You might also like

Check if Two Strings Are Rotations of Each Other in Java — Definition, Examples and Program

Learn how to check whether two strings are rotations of each other in Java using logical string comparison without using complex libraries.

Find the Shortest Word in a Sentence in Java — Definition, Examples and Program

Learn how to find the shortest word in a sentence in Java using a simple character-by-character approach without using split() or advanced string methods.

Abundant Number in Java — Definition, Examples and Program

Learn what an abundant number is in Java with definition, examples, and a clean program to check whether a number is abundant or not.

Concatenate Two Strings in Java Without Using + Operator — Examples and Program

Learn how to concatenate two strings in Java without using the + operator. This blog explains the logic with examples, a Java program, and practice challenges.

Check if a String is Empty or Null in Java — Definition, Examples and Program

Learn how to check whether a string is null or empty in Java. This blog explains the difference, common mistakes, examples, and a safe Java program with practice challenges.

Convert String to Uppercase in Java Without Using String Library

Learn how to convert a string to uppercase in Java without using built-in String methods. This blog explains the ASCII-based logic with examples, a Java program, and practice challenges.