Question

In: Computer Science

1. Obtain the following string: abcdefghijklmnopqrstuvwxyz (as input or using initialization) 2. Using recursion, write a...

1. Obtain the following string: abcdefghijklmnopqrstuvwxyz (as input or using initialization)

2. Using recursion, write a reverse function that reverses the characters in a string or character array given two indices (starting and ending). The string or the character array should reflect the reversal.

3. Read indices as input 11,18 (i.e. letters 12,19)

4. Call the reverse function to reverse letters: 12-19

5. Read indices as input 4,22 (i.e. letters 5,23)

6. Call the reverse function to reverse letters: 5,23

7. Using the reverse function, reverse the alphabet 8. Print the reversed string or character array.

Your output should contain:

abcdefghijksrqponmltuvwxyz

abcdwvutsrqponmlkjihgfexyz

zyxwvutsrqponmlkjihgfedcba

NOTE: your recursive function should modify the original string passed in as an argument, NOT make additional copies of the original string

Solutions

Expert Solution

import java.util.*;
public class Main
{
static void reverseString(char[] ch,int i,int j)
{
if (i >=j) //recursively goes until i becomes greater than j
return;
swap(ch,i,j); //swap first index and last index
reverseString(ch, i + 1,j-1); //incr && decr index call recursion
}
static char[] swap(char []arr, int i, int j)
{
char temp= arr[i]; //swapping Characters
arr[i]=arr[j];
arr[j]=temp;
return arr;
}
   public static void main(String[] args) {
   String str="abcdefghijklmnopqrstuvwxyz";
   int n = str.length();
   int i,j;
   char []ch = str.toCharArray();
   Scanner s=new Scanner(System.in);
   for(int k=0;k<2;k++){
   i=s.nextInt();
   j=s.nextInt();
reverseString(ch, i,j);
System.out.println(ch);
   }
   }
}


Related Solutions

Write a driver to get a String input from keyboard and if the input string has...
Write a driver to get a String input from keyboard and if the input string has less than 10 characters, throw a StringTooShortException. public class StringTooShortException extends Exception {     //-----------------------------------------------------------------     // Sets up the exception object with a particular message.     //-----------------------------------------------------------------     public StringTooShortException()     {         super("String does not have enough characters");     } }
Using SQL, write a table-valued function that: -- takes a space delimited string as input (Input...
Using SQL, write a table-valued function that: -- takes a space delimited string as input (Input string will be a sentance ex: "The cat is on the chair and the bear is on the chair") -- returns a table (word varchar(max), count int) (Output is a table that shows how many times each word appeared in the sentance) Where each space-delimited “word” in the string appears in the table along with the number of times it appeared in the input...
In this Exercise, you have to take a single string as input. Using this input string,...
In this Exercise, you have to take a single string as input. Using this input string, you have to create multiple queues in which each queue will comprise of separate word appeared in input string. At the end, you will again concatenate all queues to a single queue.s Example: String = “Data Structure and Algo” Q1 = D → a → t → a Q2 = S → t → r → u → c → t → u →...
Write a recursive method using Java that takes a string s as input and returns a...
Write a recursive method using Java that takes a string s as input and returns a list that contains all the anagrams of the string s. An anagram is a word formed by rearranging the letters of a different word. For instance, the word ‘cat’ is an anagram of ‘act’. Notice that the output list cannot contain duplicates.
Q20. Using C++ style string to write a program that reads a sentence as input and...
Q20. Using C++ style string to write a program that reads a sentence as input and converts each word of the sentence following the rule below: For every word in the sentence, the first letter is relocated the end of the word. Then append the string “KPU” to the word. More requirements: All letters in the output should be uppercase. More assumptions: The input sentence contains no non-alphabetic letters Sample Run: Please enter the original sentence: i LOVE to program...
Q20. Using C++ style string to write a program that reads a sentence as input and...
Q20. Using C++ style string to write a program that reads a sentence as input and converts each word of the sentence following the rule below: For every word in the sentence, the first letter is relocated the end of the word. Then append the string “KPU” to the word. More requirements: All letters in the output should be uppercase. More assumptions: The input sentence contains no non-alphabetic letters Sample Run: Please enter the original sentence: i LOVE to program...
Reversing certain segments of the alphabet, using a recursive function. (C++) 1. Obtain the following string:...
Reversing certain segments of the alphabet, using a recursive function. (C++) 1. Obtain the following string: abcdefghijklmnopqrstuvwxyz (as input or using initialization) 2. Using recursion, write a reverse function that reverses the characters in a string or character array given two indices (starting and ending). The string or the character array should reflect the reversal. 3. Read indices as input 11,18 (i.e. letters 12,19) 4. Call the reverse function to reverse letters: 12-19 5. Read indices as input 4,22 (i.e....
Write a program in python that implements quicksort, first using recursion and then without recursion.
Write a program in python that implements quicksort, first using recursion and then without recursion.
Write a function that takes a C string as an input parameter and reverses the string.
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
Write a JAVA program that reads in a string from standard input and determines the following:...
Write a JAVA program that reads in a string from standard input and determines the following: - How many vowels are in the string (FOR THE PURPOSE OF THIS PROGRAM 'Y' is NOT considered a vowel)? - How many upper case characters are in the string? - How many digits are in the string? - How many white space characters are in the string? - Modify the program to indicate which vowel occurs the most. In the case of a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT