How to find the length of a string without using the length method in java
GD.java
class GD
{
    public static void main(String args[])
    {
        String s = "KODINGWINDOW";
        int length=0;
        for(char c: s.toCharArray())
        {
            length++;    
        } 
        System.out.println("Length of a string is: "+length);  
    }
}
GD.java
class GD
{
    public static void main(String args[])
    {
        String s = "KODINGWINDOW\0";
        int length=0;
        for(int i=0; s.charAt(i)!='\0';i++)
        {
            length++;    
        } 
        System.out.println("Length of a string is: "+length);  
    }
}
GD.java
class GD
{
    public static void main(String args[])
    {
        String s1="KODINGWINDOW";
        int length=0;
        for(String s2:s1.split(""))
        {
            length++;
        }
        System.out.println("Length of a string is: "+length);  
    }
}
GD.java
import java.text.*;
class GD
{
    public static void main(String args[])
    {
        String s="KODINGWINDOW";
        int length=0;
        CharacterIterator it=new StringCharacterIterator(s);
        while(it.current()!=CharacterIterator.DONE) 
        {
            it.next();
            length++;
        }
        System.out.println("Length of a string is: "+length);  
    }
}
Output
godarda@gd:~$ javac GD.java
godarda@gd:~$ java GD Length of a string is: 12 godarda@gd:~$
Comments and Reactions