Question

Write a program in C to reverse a string using recursion. For example: reverse("abcde", ...) ->...

  1. Write a program in C to reverse a string using recursion. For example:

         reverse("abcde", ...)  -> "edcba"
    

    void reverse(char* str, int i, int s) {

    }

  2. provide output and main please

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <stdio.h>

void reverse(char* str, int i, int s) {
    if (i < s) {
        char temp = str[i];
        str[i] = str[s];
        str[s] = temp;
        reverse(str, i+1, s-1);
    }
}

int main() {
    char s[100] = "abcde";
    printf("Original string: %s\n", s);
    reverse(s, 0, 4);   // starting index is 0 and final index in string is 4
    printf("Reversed string: %s\n", s);
    return 0;
}

Original string: abcde Reversed string: edcba

Add a comment
Know the answer?
Add Answer to:
Write a program in C to reverse a string using recursion. For example: reverse("abcde", ...) ->...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program that prompts the user to input a string. The program then uses the...

    Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. #include <iostream> #include <string> using namespace std; void removeVowels(string& str); bool isVowel(char ch);...

  • In C, Reverse a string recursively. If “cat” is entered, the program should output “tac.” Use...

    In C, Reverse a string recursively. If “cat” is entered, the program should output “tac.” Use the integer “position” to keep track of where you are in the string. “str is the input string, “reversed” should end up as the newly built reversed string. void reverse_string(int position, char str1[], char reversed[]);

  • Using C, Write a function reverse which takes a string as an argument, reverses the string...

    Using C, Write a function reverse which takes a string as an argument, reverses the string and returns the reversed string. Note; you should not return a string that you created inside the reverse function! For example: Test char str[]-"hello" printf("%s", reverse (str)); Result olleh

  • Write a program that replace repeated three characters in a string by the character followed by 3...

    Write a program that replace repeated three characters in a string by the character followed by 3. For example, the string aabccccaaabbbbcc would become aabc3ca3b3cc. When there are more than three repeated characters, the first three characters will be replaced by the character followed by 3. You can assume the string has only lowercase letters (a-z). Your program should include the following function: void replace(char *str, char *replaced); Your program should include the following function: void replace(char *str, char *replaced);...

  • Write a program to reverse an integer number by using a function called reverse. The program...

    Write a program to reverse an integer number by using a function called reverse. The program reads in an integer number and prints its reverse order. Note that the function receives the number via a pointer, and uses the pointer to write the reverse number on the main function. The function MUST be used AS IS: void reverse(int *n) Language in C Bonus Problem: Number Reverser reverse c Write a program to reverse an integer number by using a function...

  • C++ NEED HELP WITH MY REVERSE STRING FUNCTION IN LINK LIST A function Reverse, that traverses...

    C++ NEED HELP WITH MY REVERSE STRING FUNCTION IN LINK LIST A function Reverse, that traverses the linked list and prints the reverse text to the standard output, without changing the linked list. ( Pass the linked list by value, you have the freedom to create a doubly linked list that is a copy of the original list in your program before you call the function) #include "pch.h" #include <iostream> #include <string.h> #include <string> using namespace std; #define MAX 512...

  • The following code is a C Program that is written for encrypting and decrypting a string....

    The following code is a C Program that is written for encrypting and decrypting a string. provide a full explanation of the working flow of the program. #include <stdio.h> int main() { int i, x; char str[100]; printf("\n Please enter a valid string: \t"); gets (str); printf ("\n Please choose one of the following options: \n"); printf ("1 = Encrypt the given string. \n"); printf("2 = Decrypt the entered string. \n"); scanf("%d",&x); // using switch case statements switch (x) {...

  • C++ Write a recursive function that reverses the given input string. No loops allowed, only use...

    C++ Write a recursive function that reverses the given input string. No loops allowed, only use recursive functions. Do not add more or change the parameters to the original function. Do not change the main program. #include <iostream> #include <string> using namespace std; //Write a recursive function 'void reverse(string &str)' that reverses the given input string. void reverse(string &str) { /*Code needed*/ } int main() {    string name = "sherry";    reverse(name);    cout << name << endl; //should...

  • HI CAN YOU PLEASE PROGRAM THIS ASSIGNMENT USING C++ Ask the user to input a string. Then sort the string in reverse lex...

    HI CAN YOU PLEASE PROGRAM THIS ASSIGNMENT USING C++ Ask the user to input a string. Then sort the string in reverse lexicographic order and print it out. Reverse lexicographic means reverse alphabetic, e.g. zyxwvuts...edcba. This would be done using the integer values of the characters (ASCIl table). Use any one of the sorting methods we saw in class. For example: if the user enters "zeta" sorting it in reverse lexicographic order would give "ztea. if the user enters "ZETA"...

  • Write a method called printReverse() that takes a string and uses recursion to print the contents...

    Write a method called printReverse() that takes a string and uses recursion to print the contents of the string in reverse order. The string itself should not be reversed; it must be left in its original form. The method has the following header:   void printReverse(String s, int i) where s is a reference to the string, and i is an integer parameter that you may use as you see fit. You do not need to code up this method as...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT