Java Program to find length of a string without using length method


In this program, You will learn how to find length of a string without using length method in Java.


String str = "Hello";

5 = str.length();


Example: How to find length of a string without using length method in Java

Run it Now
import java.util.Scanner;

class Main {
    public static void main(String args[]) {

        Scanner s = new Scanner(System.in);
        String str;

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

        int size = 0;
        char a[] = str.toCharArray();

        for (int i = 0; i < a.length; i++) {
            size++;
        }

        System.out.print("\nString Length :" + size);
    }
}

Output:

Enter a String :John Smith
String Length :10