Reverse A String In C Exercise 2

include<string.h>

void reverseString(char *str){

if (*str == '\0')

return;

reverseString(str + 1);

printf("%c", *str);

}

int main {

char str = "12345";

reverseString(str);

return 0;

}

Example


- 54321

- 12345

- 14562

- 54123

Explanation:

The correct answer is option (a). Once the recursive function reaches the end of the string, it generates the characters in reverse order by writing each character in the recursive's return path.

2. Which standard library function in C is used to reverse a string?

- strrev()

- strcpy()

- strcat()

- strstr()

Explanation:

The correct answer is option (a). In some C library functions, the standard library function strrev() is used to convert a string.

3. What happens when you pass a pointer to the reverseString function?

- The function will accurately reverse the string.

- The function will print an error message.

- The program will end.

- The program will do nothing but return.

Explanation:

The correct answer is option (c). When trying to specify a NULL pointer passed to the reverseString method, a segmentation error may occur.

4. Which is the right way to reverse a string using pointers in C?

void reverseString(char *str){

char *start = str;

char *end = str + strlen(str) - 1;

while (start < end) {

char temp = *start;

start = end;

*end = temp;

start++;

end--;

}

}

Example


void revereseString(char *str){

char *end = str + strlen(str) - 1;

while (str < end) {

char temp = *str;

str++ = end;

*end-- = temp;

}

}

Example


void reverseString(char *str){

char temp;

for(int i-0;i<strlem(str)/2;i++){

temp = str[i];

str[i] =str[strlen(str)-i-1];

str[strlen(str)-i-1]=temp;

}

}

Example


d. all of the above

Explanation:

The correct answer is option (d). Three separate techniques, including pointer arithmetic, temporary variables, and indexing, are used to convert a string exactly for all three functions.

5. What is the main purpose of strlen() in string reversal?

- To copy the string.

- To compare two strings.

- To determine the length of the string.

- To concatenate two strings.

Explanation:

The accurate choice is alternative (c). The strlen() function provides the string's length to locate the midpoint for reversing the string.

Input Required

This code uses input(). Please provide values below: