Java Program to find length of the string without using length function
Finding Length of string without using length() method
In this article we’re going to make a Java program to find length of String without using length() method . In this program first we will take the String input from user . After that we will convert String to character array by using toCharArray() . After that we will use for-each loop to count the length of the String

Algorithm:
- In this program first we will take String input from user and store in the variable called s with the help of next()
- After that we will take a for-each loop and in the paremeter of for-each loop we will convert the String to character array using toCharArray()
- After that for every variable we will take a variable named as count and increment this every time

Code in Java
Run
import java.util.Scanner; public class Main { public static void main(String[] args) { int length=0; String s = "prepinsta"; for (char c1 : s.toCharArray()) length++; System.out.println("Length of String is : "+length); } }
Output
Enter a String prepinsta Length of String is : 9
Note
The time complexity of this code is O(n) and if you are using the build in function like strlen or length the time complexity will still be same O(n)
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Login/Signup to comment
import java.util.Scanner;
class Main
{
public static void main(String [] args)
{
Scanner sc = new Scanner(System.in);
String str = sc.next();
int count=0;
for(int i=0;i=’a’ && str.charAt(i)=’A’ && str.charAt(i)<='Z')
{
count++;
}
}
System.out.println(count);
}
}
Kindly join our discord community for all your technical doubts.
class HelloWorld {
public static void main(String[] args) {
String s=”HELLO PEOPLE”;
int l=0;
for(int i=0;i<s.length();i++){
l++;
}
System.out.print(l);
}
}
import java.util.*;
public class Main
{
public static void main (String[]args)
{
String str=”Gyanendra Tiwari”;
str=str+”\0″;
int i;
for(i=0;str.charAt(i)!=’\0′;i++);
System.out.println(i);
}
}
public int StrLength(String str){
int len=0;
while(str.isEmpty!=true)
{
str=str.substring(1);
len++;
}
return len;
}
Siddhesh: Main.java
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println(“Enter String:”);
String s = sc.nextLine();
char[] c = s.toCharArray();
int len = c.length;
System.out.println(“Length of string is: ” + len);
}
}
Simple code
import java.util.*;
import java.util.Scanner;
public class Main{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
int i,length=0;
for(i=0;i<str.length();i++)
{
length++;
}
System.out.println(""+length);
}
}
import java.util.Scanner;
public class MyClass {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
System.out.println(s.length());
}
}
package stringprogram;
import java.util.Scanner;
public class String_Length_Wihout_Using_LengthFunction {
public static void main(String[] args)
{
int length=0,i=0;
Scanner sc = new Scanner(System.in);
System.out.println(“Enter a String”);
String s = sc.next();
String n=s+’\0′;
while(n.charAt(i) != ‘\0’)
{
length++;
i++;
}
System.out.println(“length of String is “+length);
}
}
Thank you prepinsta for publishing my code…