Find the length of a string without using inbuild function in Java

In this tutorial we are going to see how to get the length of any input string without using the length() function in Java.

Here is the code goes:

import java.io.*;
import java.util.Scanner;

public class lengthofstring {
     public static void main(String args[])
   {
       Scanner sc = new Scanner(System.in);
        String mystring;
        mystring = sc.next();
        int count = 0;
        for(char c : mystring.toCharArray())
        {
             count++;
        }
        System.out.println("length is: " + count);
   }
}

I hope this would help you for Interviews!

One thought on “Find the length of a string without using inbuild function in Java

Leave a Reply

Theme: Overlay by Kaira
Agurchand