1. Engineering
  2. Computer Science
  3. c 1 write a function that will crash with a...

Question: c 1 write a function that will crash with a...

Question details

C++

1. Write a function that will crash with a stack overflow.

2. Write a function to return the string argument passed to it in reverse:

char* reverseString(const char *input_string)
{
    // Your code here
}

Example:

char *result = reverseString("Hello World");

should result in the variable "result" pointing to a newly allocated string with the value
"dlroW olleH” (including a '\0' terminator). This newly allocated string should have 12 bytes allocated to it where the last byte is the null terminator.

Hints:

  • Your code should:

1. Allocate new memory for the new string.

2. Copy the strings into the new copy in reverse order.

3. Return the copy

  • You can use strlen(char *c) in string.h to find the length of a string (don’t forget that the '\0' terminator is not included in the value returned by strlen).
  • In your main function be sure to free the memory used by the string that you allocated.
  • Choose a variety of inputs to test (particularly edge cases) that your code is working properly. Edge cases include empty strings and null value. Make sure you handle these edge cases. In the case of a null string input, your function should return an empty string.
Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution