Saturday, July 3, 2021

Recursive Function And Case Change Of Letters In A Sentence

 
Design a class Change to perform string related operations. The details of the class are given below:

Class name: Change
Data Members/instance variables:
str: stores the word
newstr: stores the changed word
len: store the length of the word

Member functions:

Change(): default constructor

void inputword( ): to accept a word

char caseconvert (char ch): converts the case of the character and returns it

void recchange (int): extracts characters using recursive technique and changes its case using caseconvert () and forms a new word

void display (): displays both the words

(a) Specify the class Change, giving details of the Constructor ( ), member functions void inputword (), char caseconvert (char ch), void recchange (int) and void display (). Define the main () function to create an object and call the functions accordingly to enable the above change in the given word.

import java.io.*;
class change
{
    String str;
    static String newstr;
    int len;
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    private change()
    {
        str="";
        newstr="";
        len=0;
    }
    private void inputword()throws IOException
    {
        System.out.println("Enter the word :");
        str=br.readLine();
        len=str.length();
        rechange(0);
    }
    private char caseconvert(char ch)
    {
        if(Character.isUpperCase(ch))
        ch=Character.toLowerCase(ch);
        else if(Character.isLowerCase(ch))
        ch=Character.toUpperCase(ch);
      return ch;  
    }
    private void rechange(int n)
    {
        char x=' ';
       if(n==len)
       return;   
        char ch=str.charAt(n);
        x=caseconvert(ch);
        newstr=newstr+x;
       n++;
       rechange(n);
    }
    public static void main(String args[])throws IOException
    {
        change ob=new change();
        ob.inputword();
        System.out.println(newstr);
    }
}

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner