Answer:
See below.
Step-by-step explanation:
Let's start with the first problem: reversing a string using an iterative method in C.
-------------------------------------------------------------------------------------------------------
#include <stdio.h>
#include <string.h>
void reverse_iterative(char str[]) {
int len = strlen(str);
int start = 0;
int end = len - 1;
// Swap characters from start and end indices until they meet in the middle
while (start < end) {
char temp = str[start];
str[start] = str[end];
str[end] = temp;
start++;
end--;
}
}
int main() {
char str[30];
printf("Enter any string: ");
scanf("%[^\\]", str);
reverse_iterative(str);
printf("Reversed string is: %s\\", str);
return 0;
}
-----------------------------------------------------------------------------------------
In this code, we define the function reverse_iterative that takes a character array (str) as input. We use the strlen function from the string.h library to find the length of the string.
Then, we initialize two indices (start and end) to the beginning and end of the string, respectively. We iterate through the string, swapping the characters at the start and end indices, and incrementing start and decrementing end until they meet in the middle.
Finally, in the main function, we prompt the user to enter a string, call the reverse_iterative function to reverse the string, and print the reversed string.