In this article we are going to see how we can find the length of a string using recursion by Java programming language.
Java Program to Find the Length of a String Using Recursion
The length of the string refers to the total number of characters present in that.
For example-
A string is "BtechGeeks" Then in this string there are 10 characters. So length of string is 10.
Let’s see the program to find the length of a string using recursion.
- Java Program to Find the Length of a String Using Recursion By Using Static Input Value
- Java Program to Find the Length of a String Using Recursion By Using User Input Value
Method-1: Java Program to Find the Length of a String Using Recursion By Using Static Input Value
Approach:
- Store a string.
- Store the string and call the user-defined method
strLen( )
passing the string as parameter. - The user defined method checks if we have reached the end, else it recursively calls itself on the same string without the current character and adds 1 to the result.
- Print the result.
Program:
import java.util.*; // Main class public class Main { // Recursive method to calculate string length public static int strLen(String s) { // Checks if we have reached the end of the string if (s.equals("")) return 0; else // Calls function on a substring not including the current character return strLen(s.substring(1)) + 1; } public static void main(String[] args) { String s = "Hi!!!"; // Call the method and store the length int length = strLen(s); // Print the result System.out.println("The number of characters in the string "+s+" is "+length); } }
Output: The number of characters in the string Hi!!! is 5
Method-2: Java Program to Find the Length of a String Using Recursion By Using User Input Value
Approach:
- Ask the user to input a string.
- Store the string and call the user-defined method
strLen( )
passing the string as parameter. - The user defined method checks if we have reached the end, else it recursively calls itself on the same string without the current character and adds 1 to the result.
- Print the result.
Program:
import java.util.*; // Main class public class Main { // Recursive method to calculate string length public static int strLen(String s) { // Checks if we have reached the end of the string if (s.equals("")) return 0; else // Calls function on a substring not including the current character return strLen(s.substring(1)) + 1; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); // Ask the user to input the string System.out.print("Enter the string - "); String s = sc.nextLine(); // Call the method and store the length int length = strLen(s); // Print the result System.out.println("The number of characters in the string "+s+" is "+length); } }
Output: Enter the string - helloworld! The number of characters in the string helloworld! is 11
Want to excel in java coding? Practice with these Java Programs examples with output and write any kind of easy or difficult programs in the java language.