64 665
Assignments Done
99,2%
Successfully Done
In September 2018

Answer to Question #75814 in C for tlg

Question #75814
The program must take the reverse of each of the words inside the string and outputs the resultant string without using any string library function except strlen(), gets(), puts(). In the main function, input one string and pass it to a function named cleanString. This function removes the non-alphabetic characters and writes the rest into a new character array (i.e. non-alphabetic characters are +, - ?, *, 1,2,3,…). After receiving a cleaned array, you should call another function named reverseWords. This function takes and returns the string as a parameter. String should be arranged in a reversed order for each word. All operations in this function should take place on the same string, you shouldn’t be using a temporary array for the reverse operation. Finally, print the result in the main function.
Sample run:
Enter first string: This-?=*is__12the**23%third?[]homework--_?().
Cleaned string: This is the third homework
Reversed string: sihT si eht driht krowemoh
Expert's answer
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void cleanString(char* input, char* output)
{
int i, j, l;

l = (int)strlen(input);
j = 0;
for(i=0; i<l; i++)
{
if ((input[i]>='a' && input[i]<='z') ||
(input[i]>='A' && input[i]<='Z'))
{
output[j] = input[i];
j++;
output[j] = 0;
}
else
{
if (j>0)
{
if (output[j-1] != ' ')
{
output[j] = ' ';
j++;
output[j] = 0;
}
}
}
}
}

void reverseWords(char* words)
{
int i,j,l,k;
char c;

l = strlen(words);

i = 0;
j = 0;

while(i<l && j<l)
{
while(words[i]==' ' && i<l)
i++;

j = i;
while(words[j]!=' ' && j<l)
j++;

j--;
k = j;
while(k > i)
{
c = words[k];
words[k] = words[i];
words[i] = c;
i++;
k--;
}

i = j + 1;
}

}


int main(int argc, char* argv[])
{
char initial_str[1024];
char cleaned_str[1024];

printf("Enter first string: ");
gets(initial_str);
cleanString(initial_str, cleaned_str);
printf("Cleaned string: ");
puts(cleaned_str);
reverseWords(cleaned_str);
printf("Reversed string: ");
puts(cleaned_str);

return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions