Write a program to reverse the words of a string. Implement a function reverse (
ID: 3861877 • Letter: W
Question
Write a program to reverse the words of a string. Implement a function reverse () that reverses the string between indexes i and j inclusively. Prototype of the function is given below. void reverse (char *str, int i, int j) Example An example is given in figure 1. If we have reverse (str, 0, 4) it reverses the string between indexes of 0 and 4 from apple to elppa. You can reverse all the words of a string by finding start and end positions of each word and reversing the word. Assume that you have only uppercase and lowercase letters in the word. Sample execution is given below fox01> recitation7 Enter a string: apple orange banana Reversed string: elppa egnaro ananabExplanation / Answer
#include <stdio.h>
#include <string.h>
void reverse(char *str, int i, int j)
{
int k = 0, x, len;
char str1[10][20], temp;
for (i = 0;str[i] != ''; i++)
{
if (str[i] == ' ')
{
str1[k][j]='';
k++;
j=0;
}
else
{
str1[k][j]=str[i];
j++;
}
}
str1[k][j] = '';
/* reverses each word of a given string */
for (i = 0;i <= k;i++)
{
len = strlen(str1[i]);
for (j = 0, x = len - 1;j < x;j++,x--)
{
temp = str1[i][j];
str1[i][j] = str1[i][x];
str1[i][x] = temp;
}
}
for (i = 0;i <= k;i++)
{
printf("Reversed string: %s ", str1[i]);
}
}
void main()
{
char str[100], str1[10][20], temp;
printf("Enter the string : ");
scanf("%[^ ]s", str);
reverse(str,0,0);
}
Enter the string :
apple orange banana
Reversed string:
elppa egnaro ananab
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.