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



Write a function that accepts a sentence as a parameter, and returns the same with each of its word..

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

Write a function that accepts a sentence as a parameter, and returns the same with each of its word..

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

Write a function that accepts a sentence as a parameter, and returns the same with each of its word..

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

Write a function that accepts a sentence as a parameter, and returns the same with each of its word..

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

Write a function that accepts a sentence as a parameter, and returns the same with each of its word..

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

Post New Answer

More C Interview Questions

What are loops in c?

0 Answers  


main() { int arr[5]={23,67}; printf("%d%d%d",arr[2],arr[3],arr[4]); }

9 Answers   TCS,


Here is a good puzzle: how do you write a program which produces its own source code as output?

0 Answers  


What is a global variable in c?

0 Answers  


How can I prevent another program from modifying part of a file that I am modifying?

0 Answers  






Explain what is wrong with this program statement? Void = 10;

0 Answers  


Who is the main contributor in designing the c language after dennis ritchie?

0 Answers  


What are external variables in c?

0 Answers  


Who invented b language?

0 Answers  


Write a C program that defines a 2-dimentional integer array called A [50][50]. Then the elements of this array should randomly be initialized either to 1 or 0. The program should then print out all the elements in the diagonal (i.e. a[0][0], a[1][1],a[2][2], a[3][3], ……..a[49][49]). Finally, print out how many zeros and ones in the diagonal.

3 Answers   Infosys,


while loop contains parts a) initialisation, evalution of an expression,increment /decrement b) initialisation, increment/decrement c) condition evalution d) none of the above

0 Answers  


What's the total generic pointer type?

0 Answers  


Categories