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


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


String str = "Hello";

5 = str.length();

Example: How to find the length of a string without using length method in java.

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("String length:" + size);
    }
}

Output:

Enter a string:Xiith.com
String length:9