Pages

Find length of string without using length method

Find length of string without using length method

  • We can find the length of given string by converting the given string into array then iterate the array by using for-each loop.
public class FindLengthStringWithoutLengthMethod {
public static void main(String[] args) {
String str = "ABCDE F";

String[] arr = str.split("");

int count = 0;

for (String v : arr) {
count++;
}


System.
out.println("length of string using split=" + count);

char[]
carr = str.toCharArray();

int
charCount = 0;
for (char v : carr) {
charCount
++;
}

System.
out.println("length of string using toCharArray=" + charCount);
}
}
Output:
Find length of string without using length method
Find length of string without using length method
Please comment below to feedback or ask questions.

No comments:

Post a Comment

Please comment below to feedback or ask questions.