How to find the length of a string without using the length method in java
Codeaft.java
classCodeaft{publicstaticvoidmain(Stringargs[]){Strings="CODEAFT";intlength=0;for(charc:s.toCharArray()){length++;}System.out.println("Length of a string is: "+length);}}
Codeaft.java
classCodeaft{publicstaticvoidmain(Stringargs[]){Strings="CODEAFT\0";intlength=0;for(inti=0;s.charAt(i)!='\0';i++){length++;}System.out.println("Length of a string is: "+length);}}
Codeaft.java
classCodeaft{publicstaticvoidmain(Stringargs[]){Strings1="CODEAFT";intlength=0;for(Strings2:s1.split("")){length++;}System.out.println("Length of a string is: "+length);}}
Codeaft.java
importjava.text.*;classCodeaft{publicstaticvoidmain(Stringargs[]){Strings="CODEAFT";intlength=0;CharacterIteratorit=newStringCharacterIterator(s);while(it.current()!=CharacterIterator.DONE){it.next();length++;}System.out.println("Length of a string is: "+length);}}
Output
codeaft@codeaft:~$ javac Codeaft.java codeaft@codeaft:~$ java Codeaft
Length of a string is: 12
codeaft@codeaft:~$
Dear User, Thank you for visitng Codeaft. If you are interested in technical articles, latest technologies, and our journey further, please follow us on LinkedIn.