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
Explain what could possibly be the problem if a valid function name such as tolower() is being reported by the c compiler as undefined?
What is unsigned int in c?
Do you know pointer in c?
How important is structure in life?
A routine usually part of the operation system that loads a program into memory prior to execution a) linker b) loader c) preprocessor d) compiler
Why main is not a keyword in c?
Explain the difference between malloc() and calloc() function?
With the help of using classes, write a program to add two numbers.
Explain what does a function declared as pascal do differently?
What is bin sh c?
Is c is a middle level language?
Badboy is defined who has ALL the following properties: Does not have a girlfriend and is not married. He is not more than 23 years old. The middle name should be "Singh" The last name should have more than 4 characters. The character 'a' should appear in the last name at least two times. The name of one of his brothers should be "Ram" Write a method: boolean isBadBoy(boolean hasGirlFriend , boolean isMarried, int age , String middleName , String lastName , String[] brotherName); isHaveGirlFriend is true if the person has a girlfriend isMarried is true if the person is married age is the age of the person middleName is the middle name of the person lastName is the last name of the person brotherName is the array of the names of his brothers
What is a 'null pointer assignment' error? Explain what are bus errors, memory faults, and core dumps?
Who is the main contributor in designing the c language after dennis ritchie?
regarding pointers concept