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 is chain pointer in c?

600


How can my program discover the complete pathname to the executable from which it was invoked?

658


What is the difference between a string and an array?

703


Explain with the aid of an example why arrays of structures don’t provide an efficient representation when it comes to adding and deleting records internal to the array.

2653


Function which gives a pointer to a binary trees const an integer value at each code, return function of all the nodes in binary tree.?

622






What are linked lists in c?

647


What does the c preprocessor do?

615


A character flag or control mechanism that delineates one data item from another a) variable b) constant c) delimiter d) call by reference

629


What is sizeof int?

630


In a byte, what is the maximum decimal number that you can accommodate?

624


Which operators cannot be overloaded a) Sizeof b) .* c) :: d) all of the above

656


What is the difference between Printf(..) and sprint(...) ?

780


Who developed c language?

637


How can I implement a delay, or time a users response, with sub-second resolution?

617


What is pointer to pointer in c language?

593