Write a recursive function to print the punctuation in a string containing a sen
ID: 3607727 • Letter: W
Question
Write a recursive function to print the punctuation in a string containing a sentence.
For example, for the sentence S, where:
S = “Every day is a gift!”, said the parent to their child.
The recursive function punctuation(S) will print:
“!”,.
Write the base cases and recurrence relationship for the function in the comments that describe your function, as you did for problem A above.
You should embed the function in a program that calls the recursive function to find the punctuation in different sentences input by the user. Your program should obtain a sentence from the user via the console. Your program should also contain a continuation loop that prompts the user to determine if they would like to continue.
One sample run of your program should be similar to the following:
Enter a Sentence: “Every day is a gift!”
The punctuation is: “!”
Would you like to continue? (Enter ‘Y’ or ‘N’): N
Your program should check for the following punctuation marks (specified between the braces {} below with a space in between each punctuation mark):
{ . , “ ‘ ! ? – : ; }
Note, we assume that the double quote: “ is equivalent to: ”
And the single quote: ‘ is equivalent to: ’
Explanation / Answer
Explanaion:
Steps:
1. We pass a String to a Recursive function.
2. Base case is when the length of string is 0 or if null is passed
3. we check with the first index of string if it is
(specified between the braces {} below with a space in between each punctuation mark)
{ . , “ ‘ ! ? – : ; }
4. If yes, we print it and then pass string(index+1,length) i.e we leave out the first character as it is already checked.
In this way we keep on checking for punctuation and the length of string decreases in each recursive call.
5. when the base case is reached, the string is returned.
In the main we have a do while loop which checks if the user wants to continue or not.
I hope the algo is clear. The programming language was'nt mentioned, I have used java.This can be implemented anywhere. If you have any doubts, Kindly comment. Find the outputs below.
***************************************************************************************************************************
Code:
import java.util.Scanner;
public class MyClass {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
String c;
do{
System.out.print("Enter a sentence: ");
String input =sc.nextLine();
System.out.print("The punctuation is: ");
printPunctuation(input);
System.out.print(" Would you like to continue? (Enter ‘Y’ or ‘N’): ");
c=sc.nextLine();
}while(c.equals("Y"));
}
public static String printPunctuation(String str)
{
if (str == null || str.equals(""))
{
return str;
}
else if(str.charAt(0)=='.' ||
str.charAt(0)==',' || str.charAt(0)==''' ||
str.charAt(0)=='"' || str.charAt(0)=='!' || str.charAt(0)=='?' ||
str.charAt(0)=='-' || str.charAt(0)==':' || str.charAt(0)==';')
{
System.out.print(str.charAt(0));
return printPunctuation(str.substring(1, str.length()));
}
else
return printPunctuation(str.substring(1, str.length()));
}
}
***************************************************************************************************************************
Sample output:
********************************************************************************************************************************
I hope this helps you.
If you find my answer helpful,
Kindly rate the answer.
All the best :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.