Write a function that accepts a sentence as a parameter, and
returns the same with each of its words reversed. The
returned sentence should have 1 blank space between each
pair of words.
Demonstrate the usage of this function from a main program.
Example:
Parameter: “jack and jill went up a hill” Return Value:
“kcaj dna llij tnew pu a llih”

Answer Posted / ep

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>

char *wreverse( char *phrase )
{
char *rev = NULL, c;
size_t len = strlen(phrase);
int i, j, ri, ws;

if (!len) return NULL;

if ( (rev = calloc( len + 1, sizeof(char) )) == NULL ) {
perror("calloc failed");
exit(2);
};

i = 0; ri = 0; ws = 0;
while ( i < len ) {
while ( i<len && isalpha(*(phrase+i)) ) i++;
if ( i<=len ) {
for ( j=i-1; j>=ws; j-- ) {
*(rev+ri) = *(phrase+j);
ri++;
}
for ( ; i<len && (!isalpha(*(phrase+i))) ; i++ ) {
*(rev+ri) = *(phrase+i);
ri++;
}
ws = i;
}
}

return rev;
}

int main()
{
char p[] = "jack and jill went up a hill";
char *r = NULL;

r = wreverse(p);
printf("%s\n", wreverse(p) );
free(r);

return 0;
}

Is This Answer Correct ?    12 Yes 2 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What are local static variables?

622


Are the expressions * ptr ++ and ++ * ptr same?

671


i have to apply for rbi before that i need to know the the syllabus for the entrance questions. whethet it may be aps or techinical

1848


How many identifiers are there in c?

587


Explain what happens if you free a pointer twice?

615






Why doesnt long int work?

615


Explain how do you override a defined macro?

590


What are variables and it what way is it different from constants?

793


Why do we need a structure?

593


What is static memory allocation?

611


Draw a flowchart to produce a printed list of all the students over the age of 20 in a class .The input records contains the name and age of students. Assume a sentinel value of 99 for the age field of the trailer record

4748


Which is better pointer or array?

606


What are structural members?

576


What are the string functions? List some string functions available in c.

609


What is the symbol indicated the c-preprocessor?

700