Problem:
Write a java method returns the length of a string using iteration.
Output:
Not applicable.
Solution:
public class string_length_interation
{
public static int length (String str)
{
char c = str.charAt(0);
int count=0;
while (c != '\0')
{
count++;
c = str.charAt(count);
}
return count;
}
public static void main(String[] args)
{
System.out.println(length("Few"));
}
}
No comments :
Post a Comment