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