Data Structure Questions and Answers – String Reversal using Recursion

This set of Data Structure Multiple Choice Questions & Answers (MCQs) focuses on “String Reversal using Recursion”.

1. Consider the following iterative implementation used to reverse a string:

#include<stdio.h>
#include<string.h>
void reverse_string(char *s)
{
     int len = strlen(s);
     int i,j;
     i=0;
     j=len-1;
     while(______)
     {
         char tmp = s[i];
         s[i] = s[j];
         s[j] = tmp;
         i++;
         j--;
     }
}

Which of the following lines should be inserted to complete the above code?
a) i > j
b) i < len
c) j > 0
d) i < j
View Answer

Answer: d
Explanation: The line “i < j” should be inserted to complete the above code.

advertisement
advertisement

2. What is the output of the following code?

#include<stdio.h>
#include<string.h>
void reverse_string(char *s)
{
     int len = strlen(s);
     int i,j;
     i=0;
     j=len-1;
     while(i < j)
     {
         char tmp = s[i];
         s[i] = s[j];
         s[j] = tmp;
         i++;
         j--;
     }
}
int main()
{
      char s[100] = "reverse";
      reverse_string(s);
      printf("%s",s);
      return 0;
}

a) ersevre
b) esrever
c) eserver
d) eresevr
View Answer

Answer: b
Explanation: The program reverses the string “reverse” and prints “esrever”.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

3. What is the time complexity of the above code used to reverse a string?
a) O(1)
b) O(n)
c) O(n2)
d) O(n3)
View Answer

Answer: b
Explanation: The time complexity of the above code used to reverse a string is O(n).
advertisement

4. What does the following code do?

#include<stdio.h>
#include<string.h>
void reverse_string(char *s)
{
     int len = strlen(s);
     int i,j;
     i=0;
     j=len-1;
     while(i < j)
     {
         char tmp = s[i];
         s[i] = s[j];
         s[j] = tmp;
         i++;
         j--;
     }
}
int main()
{
      char s[100] = "abcdefg";
      char t[100];
      strcpy(t,s);
      reverse_string(s);
      if(strcmp(t,s) == 0)
        printf("Yes");
      else
        printf("No");
      return 0;
}

a) Copies a string to another string
b) Compares two strings
c) Reverses a string
d) Checks if a string is a palindrome
View Answer

Answer: d
Explanation: The main purpose of the above code is to check if a string is a palindrome.
advertisement

5. What is the output of the following code?

#include<stdio.h>
#include<string.h>
void reverse_string(char *s)
{
     int len = strlen(s);
     int i,j;
     i=0;
     j=len-1;
     while(i < j)
     {
         char tmp = s[i];
         s[i] = s[j];
         s[j] = tmp;
         i++;
         j--;
     }
}
int main()
{
      char s[100] = "rotator";
      char t[100];
      strcpy(t,s);
      reverse_string(s);
      if(strcmp(t,s) == 0)
        printf("Yes");
      else
        printf("No");
      return 0;
}

a) Yes
b) No
c) Runtime error
d) Compile time error
View Answer

Answer: a
Explanation: The program checks if a string is a palindrome. Since the string rotator is a palindrome, it prints “yes”.

6. Consider the following recursive implementation used to reverse a string:

void recursive_reverse_string(char *s, int left, int right)
{
     if(left < right)
     {
         char tmp = s[left];
         s[left] = s[right];
         s[right] = tmp;
         _________;
     }
}

Which of the following lines should be inserted to complete the above code?
a) recursive_reverse_string(s, left+1, right+1)
b) recursive_reverse_string(s, left-1, right-1)
c) recursive_reverse_string(s, left+1, right-1)
d) recursive_reverse_string(s, left-1, right+1)
View Answer

Answer: c
Explanation: The line “recursive_reverse_string(s, left+1, right-1)” should be inserted to complete the above code.

7. What is the output of the following code?

#include<stdio.h>
#include<string.h>
void recursive_reverse_string(char *s, int left, int right)
{
     if(left < right)
     {
         char tmp = s[left];
         s[left] = s[right];
         s[right] = tmp;
         recursive_reverse_string(s, left+1, right-1);
     }
}
int main()
{
     char s[100] = "recursion";
     int len = strlen(s);
     recursive_reverse_string(s,0,len-1);
     printf("%s",s);
     return 0;
}

a) recursion
b) nsoirucer
c) noisrcuer
d) noisrucer
View Answer

Answer: d
Explanation: The program prints the reversed string of “recursion”, which is “noisrucer”.

8. How many times is the function recursive_reverse_string() called when the following code is executed?

#include<stdio.h>
#include<string.h>
void recursive_reverse_string(char *s, int left, int right)
{
     if(left < right)
     {
         char tmp = s[left];
         s[left] = s[right];
         s[right] = tmp;
         recursive_reverse_string(s, left+1, right-1);
     }
}
int main()
{
     char s[100] = "madam";
     int len = strlen(s);
     recursive_reverse_string(s,0,len-1);
     printf("%s",s);
     return 0;
}

a) 3
b) 4
c) 5
d) 6
View Answer

Answer: a
Explanation: The function recursive_reverse_string() is called 3 times when the above code is executed.

9. What is the time complexity of the above recursive implementation used to reverse a string?
a) O(1)
b) O(n)
c) O(n2)
d) O(n3)
View Answer

Answer: b
Explanation: The time complexity of the above recursive implementation used to reverse a string is O(n).

10. In which of the following cases is the reversal of a string not equal to the original string?
a) Palindromic strings
b) Strings of length 1
c) Empty String
d) Strings of length 2
View Answer

Answer: d
Explanation: String “ab” is a string of length 2 whose reversal is not the same as the given one. Palindromic Strings, String of length 1 and Empty Strings case – the reversal is the same as the one given.

Sanfoundry Global Education & Learning Series – Data Structures & Algorithms.

To practice all areas of Data Structures & Algorithms, here is complete set of 1000+ Multiple Choice Questions and Answers.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.