Question

In: Computer Science

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. 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

What I've got so far:

(Just a prototype) -- Doesn't completely follow the instructions, Just showing the general direction I've been trying to go in.

// I'm a bit confused on how to transform the string from "abcdefg" to "abedcfg"

// Alright, Here it is...

# include
# include
using namespace std;


void reverse(string& alpha, int x, int y)
{


   if ((x - 1) == y)
   {
       return;
   }

   cout << alpha[y - 1];

   reverse(alpha, x , y - 1);
  

}

int main()
{
  


   string alpha = "abcdefg";

reverse(name, 3, 5);

// My output: edc

// The output I wanted: abedcfg ( When cout << alpha )

  
   return 0;
}

Solutions

Expert Solution

Code:

# include<iostream>
#include<string>
using namespace std;
void reverse(string& alpha, int x, int y)     //function reverse
{
   char t;

   if ((x>y))
   {                                  //here does not use any additional string.
       return;
   }

   t=alpha[x];
   alpha[x]=alpha[y];
   alpha[y]=t;
   reverse(alpha, x+1 , y - 1);
}

int main()
{
   string alpha;
   int u,k;
   cout<<"enter string:";          //reading input string.
   cin>>alpha;
   cout<<"enter starting index:"; //reading starting index.
   cin>>u;
   cout<<"enter ending index:";     //reading ending index.
   cin>>k;
   reverse(alpha, u, k);
   cout<<endl<<"your modified string is "<<alpha;      //printing modified string
    return 0;
}

Output 1:

Output 2:

Output 3:


Related Solutions

c++ using recursive no loops (for ,while ..ect)not allowed Write a recursive function ‘bool palindrome(string s)’...
c++ using recursive no loops (for ,while ..ect)not allowed Write a recursive function ‘bool palindrome(string s)’ that returns true if s is a palindrome and false if not. #5: Write a recursive function 'void reverse(string &word)' that reverses the given input string. string name = "damian"; reverse(name); cout << name << endl; //should display "naimad". #7: Write a function 'int numTwos(int n)' which returns the number of 2's in the base-4 expansion of n. cout << numTwos(2170) << endl; //...
Write a short recursive C++ function that determines if a string s is a palindrome, that...
Write a short recursive C++ function that determines if a string s is a palindrome, that is, it is equal to its reverse. For example,"racecar" and "gohangasalamiimalasagnahog" are palindromes. Please include the pseudo code so that I can understand better with simple English as much as possible.
C++ Recursive Functions: Please call functions in a main function as well. 1. A recursive function...
C++ Recursive Functions: Please call functions in a main function as well. 1. A recursive function that print the reverse of a string. (e.g., void printReverse(string exp)). For example, if exp =”coding”, then the function should print out “gnidoc”. 2. Implement a non-recursion-based binary search function. Convert this function into a recursion-based function. 3. Implement a recursive and non-recursive Fibonacci function.
Write a recursive Racket function "remove-char" that takes two string parameters, s and c, and evaluates...
Write a recursive Racket function "remove-char" that takes two string parameters, s and c, and evaluates to string s with all occurrences of c removed. The string c is guaranteed to be a length-1 string; in other words a single character string. For example (remove-char "abc" "b") should evaluate to "ac". Here is pseudocode that you could implement.
For C++ Function 1: Write a recursive function to perform a sequential search on a set...
For C++ Function 1: Write a recursive function to perform a sequential search on a set of integers The function will require an array parameter and the number to look for. These are the minimal parameter requirements The function should take an array of any size Function 2: Write a recursive function that will convert an integer (base 10) to binary The function should only have an integer parameter Have the function write the binary number to the console You...
Part 1: Write a recursive function that will calculate Fibonacci numbers using a recursive definition. Write...
Part 1: Write a recursive function that will calculate Fibonacci numbers using a recursive definition. Write a short program to test it. The input of this program must be a positive integer n; the output is the corresponding Fibonacci number F(n) Part 2: Write an iterative function to calculate Fibonacci numbers. Write a test driver for it. The input of this program must be a positive integer n; the output is the corresponding Fibonacci number F(n). Part 3: Write a...
In Java, write a recursive function that accepts a string as its argument and prints the...
In Java, write a recursive function that accepts a string as its argument and prints the string in reverse order. Demonstrate the function in a driver program.
Give pseudocode for a recursive function that sorts all letters in a string. For example, the...
Give pseudocode for a recursive function that sorts all letters in a string. For example, the string "goodbye" would be sorted into "bdegooy". Java
Give pseudocode for a recursive function that sorts all letters in a string. For example, the...
Give pseudocode for a recursive function that sorts all letters in a string. For example, the string "goodbye" would be sorted into "bdegooy". Python
Write a recursive function to check if a string whose each character is stored in a...
Write a recursive function to check if a string whose each character is stored in a separate node in a doubly linked list, is a palindrome. Use the code available from DoublyLinkedList.hpp on our Github. // // Doubly-linked list with 2 dummy nodes // #pragma once #include <stdexcept> template<typename T> struct Node { T data; Node<T>* next; Node<T>* prev; Node() = delete; // Intentionally no default constructor Node( const T & element ) : data( element ), next( nullptr ),...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT