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”
Answers were Sorted based on User's Feedback
Answer / jeke kumar gochhayat
#include<stdio.h>
#include<string.h>
char *strrev1(char *st);
void main()
{
char st1[30];
char *pt1;
printf("enter the sentence");
gets(st1);
pt1=strrev1(st1);
puts(pt1);
}
char *strrev1(char *st)
{
int i;
char *pt=st;
for(i=0;st[i]!='\0';i++);
st[i]=' ';
st[i+1]='\0';
i=0;
for(;st[i]!='\0';i++)
{
for(;st[i]!=' ';i++);
st[i]='\0';
strrev(pt);
pt=st+i+1;
st[i]=' ';
}
return(st);
}
| Is This Answer Correct ? | 37 Yes | 5 No |
Answer / ayas kumar das
#include"stdio.h"
#include"string.h"
main()
{
char a[200],b[20][20];
char c[200],d[20][20];
int i,j=0,k=0,y=0,l;
memset(b,0,sizeof(b)); //initializing the array
printf("enter your own string:");
//Enter the string which you want
gets(a);
for(i=0;i<strlen(a);i++)
{
if(a[i]!=' ')
{
b[j][k]=a[i];
k++;
y++;
}
else
{
if(y!=0)
//if there are more than one space
between two words
{
while(a[i]==' ')
{
i++;
}
i--;
k=0;
j++;
}
else
//if initialy there are more than one space
{
while(a[i]==' ')
{
i++;
}
i--;
y++;
}
}
}
for(i=0;strlen(b[i]);i++)
{
k=strlen(b[i]);
for(l=k;l>=0;l--)
{
printf("%c",b[i][l]); //here reversing
each word
}
printf(" ");
}
return 0;
}
//enter "jack and jill went up a hill"
| Is This Answer Correct ? | 13 Yes | 2 No |
Answer / 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 |
Answer / gunapala
start=0;
for(i=0;str[i]!='\n';i++)
{
if(str[i]!=' ')
{
for(j=i;j>=start;j--)
{
b[k++]=str[j];
}
start=start+1;
}
printf("/s",b);
}
}
}
| Is This Answer Correct ? | 1 Yes | 1 No |
Answer / vinay
public class Reversesentence {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.println("Enter the sentence");
String str=s.nextLine();
String[] str2=str.split(" ");
int l=str2.length;
for(int i=0;i<l;i++) {
StringBuffer sb=new StringBuffer(str2[i]);
StringBuffer revstr = sb.reverse();
System.out.print(" "+revstr);
}
}
}
| Is This Answer Correct ? | 0 Yes | 0 No |
How pointer is different from array?
What does extern mean in a function declaration?
Given an array of numbers, except for one number all the others occur twice. Give an algorithm to find that number which occurs only once in the array.
Describe the difference between = and == symbols in c programming?
proc() { static i=10; printf("%d",i); } If this proc() is called second time, what is the output?
What are the uses of pre-processor directives?
C program to read the integer and calculate sum and average using single dimensional array
Which driver is a pure java driver
Why calloc is better than malloc?
#include main() { char s[] = "Bouquets and Brickbats"; printf(" %c, ",*(&s[2])); printf("%s, ",s+5); printf(" %s",s); printf(" %c",*(s+2)); }
What is sizeof array in c?
how do u find out the number of 1's in the binary representation of a decimal number without converting it into binary(i mean without dividing by 2 and finding out the remainder)? three lines of c code s there it seems...can anyone help