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

program to find error in linklist.(i.e find whether any node point wrongly to previous nodes instead of next node)

1721


What are the types of i/o functions?

790


How can I change the size of the dynamically allocated array?

741


What is a 'null pointer assignment' error?

815


Is main is user defined function?

694






Is array name a pointer?

696


Differentiate call by value and call by reference?

646


Is it possible to execute code even after the program exits the main() function?

929


Write a program to print fibonacci series without using recursion?

712


What is header file in c?

706


What is the purpose of the statement: strcat (S2, S1)?

722


what is associativity explain what is the precidence for * and & , * and ++ how the folloing declaration work 1) *&p; 2) *p++;

2153


.main() { char *p = "hello world!"; p[0] = 'H'; printf("%s",p); }

821


What do you mean by dynamic memory allocation in c? What functions are used?

760


A function can make the value of a variable available to another by a) declaring the variable as global variable b) Passing the variable as a parameter to the second function c) Either of the two methods in (A) and (B) d) binary stream

781