The length of the string is easy to define , it is the number of characters in the string including white spaces .
Java make the things easy as it provides the built in length method for the String class (java.lang.String) which is a final class .
for example :
String demo = " This is java built in method example "
int length= demo.length();
But what happens , if we don't want to use the java built in method to calculate the length of the string .
Then there is also a way to calculate the length of the string
Logic:
Initialize two variables and keep the condition of the loop always true. Iterate the whole string using one variable ,while use other variable as counter to count the length of the string .When the string completes , then String.charAt() will throw an exception as array index out of range exception , which is caught in the catch block.
So in the below example , there are two variables i and c , here i is used to iterate through the string while c is used as a counter to calculate the length of the string .
Please note that Array index out of bounds and array index out of range are different exceptions
And the other point to be learn in the code is that we can not put the return statement in the catch block of the try catch block.
We need to put the return statement after the catch block completes.
Demo :
Please find the code below :
Java make the things easy as it provides the built in length method for the String class (java.lang.String) which is a final class .
for example :
String demo = " This is java built in method example "
int length= demo.length();
But what happens , if we don't want to use the java built in method to calculate the length of the string .
Then there is also a way to calculate the length of the string
Logic:
Initialize two variables and keep the condition of the loop always true. Iterate the whole string using one variable ,while use other variable as counter to count the length of the string .When the string completes , then String.charAt() will throw an exception as array index out of range exception , which is caught in the catch block.
So in the below example , there are two variables i and c , here i is used to iterate through the string while c is used as a counter to calculate the length of the string .
Please note that Array index out of bounds and array index out of range are different exceptions
And the other point to be learn in the code is that we can not put the return statement in the catch block of the try catch block.
We need to put the return statement after the catch block completes.
Demo :
Please find the code below :
public class StringLength { static int i,c,res; static int length(String s) { try { for(i=0,c=0;0<=i;i++,c++) s.charAt(i); } catch(Exception e) //Array index out of bounds and array index out of range are different exceptions { System.out.print("length is "); // we can not put return statement in catch } return c; } public static void main (String args[]) { System.out.println("Original String is : "); System.out.println("Alive is awesome "); res=StringLength.length("Alive is awesome "); System.out.println( res); } }