Possible Duplicate:
String length without using length() method in java
I need to get the length of a string (no of characters present) in Java using a for
loop and without using any methods like length()
.
import java.io.*;
import java.util.*;
public class reversestring
{
public static void main(String arg[])throws IOException
{
String s;
int i=0,j=0,k=0;
DataInputStream in=new DataInputStream(System.in);
System.out.println("Enter ur string : ");
s=in.readLine();
char ar[]=s.toCharArray();
System.out.println("Length of the string is : ");
for(j=ar[i];j!='\0';i++)
{
k++;
}
System.out.println(+k);
}
}
I wrote this program, but I am not getting the answer. What is wrong with it?