Recursive Programs On Strings PDF
Recursive Programs On Strings PDF
import java.io.*;
class Sample
{
static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
Reversing a String
Recursive Method 1 Recursive Method 2 Recursive Method 3
Without Return Type With Return Type
Note: In some of the questions related to recursion, you will find Iterative Method
mentioned in the question that you don’t need to write the main() method.
String r="";
String rev(String s)
But, the students are advised to write the main() method after writing the
{
above recursive methods, while they are practicing the programs. The main
int i=0;
method is nothing but a few lines calling the above created methods.
while(i<s.length())
{
An example of the main method for the above program is given below.
char ch=s.charAt(i);
r=r+ch;
public static void main()throws IOException
i++;
{
}
Sample ob=new Sample();
return r;
ob.display();
}
}
Note: In the First example we are using a recursive function without any return type and we are not saving the
reverse of the String in any number. We are just printing the characters as we get them in reverse order due to
the LIFO property of the stack used in recursion.
In the 2nd and 3rd example we are using a recursive function with a return type and we are saving the reverse of
the String in an instance variable 'r' and we are returning the result at the end. The difference in the approach of
the 2nd and the 3rd method is that the 2nd method extracts characters from the beginning of the String and uses
the LIFO property of the stack used in recursion to reverse the word whereas, the 3rd method extracts characters
from the end of the String and adds this to the variable 'r' and does not utilize the LIFO property of the stack used
in recursion.
In the above code, we are extracting the characters from the beginning of the String passed as parameter.
After storing it in the variable 'ch', you write the code of the operation you want to perform with that character.
You can also send the character to another function which will perform some task with that character.
Instead of writing the case conversion code inside the recursive function, you can also send that character to
another function which is performing the case conversion. An example of this is given below: (ISC 2010)
The printing statements which are inside the else clause can also be written inside the display () function if we
don’t write them in the recursive function. The statements will be written after the statement countCase(s,0);
and in that case we won’t write any else part in the recursive function.
In the above program, we have asked the user to enter a number, but have stored it as a String and not an
Integer.
Inside the recursive method alpha() we have written a for-loop which will run from the starting of the String till
the end for every English alphabets i.e. this for loop will execute 26 times.
In the above recursive method vowFirst(), we are returning a (-1) when we are not getting any vowel. If we get
a vowel then we are returning that position.
Removing the return keyword from within the if-block allows the recursive function to find the position of the last
vowel. Similarly, Removing the break keyword from within the if-block allows the Iterative function to find the
position of the last vowel.
14. Printing and Counting the Double letter sequence present in a sentence
If the characters are consecutive as in the English Alphabets, then the difference in the ASCII values of the 2nd
character and the 1st character will be one. This is what we are checking in the recursive function.
16. Forming a new word by taking first character of every word in a sentence
As soon as we are encountering a space, we are adding the character just next to it to the variable 'newstr'.
Note: Before sending the String to the recursive function, we have added a space before the String.
String piggy=""; {
int vowFirst(String s, int i) char ch=s.charAt(i);
{ piggy=piggy+ch;
if(i<s.length()) piglatin(s,i+1,q);
{ }
char ch=s.charAt(i); }
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||
ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U') void display()throws IOException
{ {
return i; System.out.print("Enter any word : ");
} String s=br.readLine();
return (vowFirst(s,i+1)); int len=s.length();
} int p=vowFirst(s,0);
return 0; piglatin(s,p,len);
} piglatin(s,0,p);
piggy=piggy+"ay";
void piglatin(String s, int i, int q) System.out.println("The Piglatin of the word =
{ "+piggy);
if(i<q) }
import java.io.*; }
class Recursion void display()throws IOException
{ {
static BufferedReader br=new BufferedReader(new System.out.print("Enter any string : ");
InputStreamReader(System.in)); String s=br.readLine();
int cap[]=new int[26]; System.out.println("Character\t\tFrequency");
int sm[]=new int[26]; freqChar(s,0);
Recursion() for(int i=0;i<26;i++)
{ {
for(int i=0;i<26;i++) if(cap[i]!=0)
{ System.out.println((char)(i+65)+"\t\t\t"+cap[i]);
cap[i]=0; }
sm[i]=0; for(int i=0;i<26;i++)
} {
} if(sm[i]!=0)
void freqChar(String s, int i) System.out.println((char)(i+97)+"\t\t\t"+sm[i]);
{ }
if(i<s.length()) }
{
char ch=s.charAt(i); public static void main()throws IOException
if(ch>='A'&&ch<='Z') {
cap[ch-65]++; Recursion ob=new Recursion();
if(ch>='a'&&ch<='z') ob.display();
sm[ch-97]++; }
freqChar(s,i+1); }
}
In the above example, the array cap[] is storing the frequency of the capital letters present in the string, and the
array sm[] is storing the frequency of the small letters present in the string. In the display() method, we are
first displaying the frequencies of the capital letters and then the small letters.
In the code given on the left , we are extracting the characters from the beginning of the String passed as
parameter and checking whether that character stored in 'ch' is a space or not. If the character is not a space,
then we add it to the String variable 'w'. When the character is a space, then we have got a word, and hence you
write what you want to do with the word stored in 'w' in the else-block.
Important Note: While using the above code, after entering the String from the user, you need to add a space at
its end before passing it on to the recursive method.
Note: We have added a space after the inputted sentence in the display () method.
The change in the line of adding the character i.e. w=ch+w; reverses the word by adding the character before
rather than after.
In the code for removing a word, the recursive function is extracting one word at a time and adding it to the new
string whenever the word is not equal to the word we want to remove.
Note: We need to send the word we want to replace and also the new word into the recursive function as a
parameter.
The recursive function is extracting one word at a time and checking whether that word is the same as the word
we want to replace or not. If yes, then the word is replaced with the new word and added to the new word.
The recursive function word() is extracting one word at a time and sending it to the function piglatin(), which is
first finding the position of the first vowel of each word and then doing the needful for converting the word into a
piglatin.
Note: Here the function piglatin() is non-recursive i.e. iterative. Also, we have added a space after the inputted
sentence in the display() method.