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.
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.
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
Give a recursive algorithm to solve the following recursive function. f(0) = 0;    f(1) = 1;...
Give a recursive algorithm to solve the following recursive function. f(0) = 0;    f(1) = 1;   f(2) = 4; f(n) = 2 f(n-1) - f(n-2) + 2; n > 2 b) Solve f(n) as a function of n using the methodology used in class for Homogenous Equations. Must solve for the constants as well as the initial conditions are given.
C++ Write a recursive function that computes and returns the product of the first n >=1...
C++ Write a recursive function that computes and returns the product of the first n >=1 real numbers in an array.
Write a recursive function (using Matlab) moreFactors(a,b,fact) that does the following: 1. Takes as an input...
Write a recursive function (using Matlab) moreFactors(a,b,fact) that does the following: 1. Takes as an input 3 positive integers. 2. Of the two integers a and b, the function returns the integer that has the most factors fact. 3. If both integers a and b have the same amount of factors fact, the function will return the larger integer. Test your function with the following: >> result=moreFactors(24,32,3) result = 24 (24 = 3^1 · 2^3 , 32 = 2^5 )...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT