Watch java videos

WAP to print the initials of string recursively.

A class Initials Print has been defined to print the initials of the name. Some of the members of the class are given below:
Class name : InitialsPrint
Data members/instance variables:

String initials : to store the initials of the name
Member functions:
initialsPrint(): constructor to assign blank to initials. Downloaded from

void initRecursive(String s, int i): to store the initials of the name in the variable initials as shown in the sample output using Recursive technique.
Sample Input: Narendra Damodardas Modi
Sample Output: N.D.M.
display(): to accept full name from the user and print the initials
of string as shown in the sample output.
Specify the class Initials Print the initials of string giving details of the constructor, void initRecursive(String s, int i) and void display(). Write main() to create and object and call the respective methods.

import java.util.*;
public class InitialsPrint
{
    String initials;
    InitialsPrint()
    {
        initials="";
    }

    void initRecursive(String s,int i)
    {
        if(i<s.length())  
        {     char ch=s.charAt(i);   
            if(ch == ' ')       
                initials+=s.charAt(i+1)+".";   
            initRecursive(s,i+1); 

        }
    }
    void display()
    {
        System.out.println("The Initials of the string are ");
        System.out.println(initials);
    }

    public static void main()
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the string");
        String sen=sc.nextLine();
        sen=" "+sen;
        InitialsPrint obj=new InitialsPrint();
        obj.initRecursive(sen,0);
        obj.display();
    }
}
initials of  string

Download free Sample papers

LEAVE A REPLY

Please enter your comment!
Please enter your name here