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 is include directive in c?

0 Answers  


What are control structures? What are the different types?

0 Answers  


Are local variables initialized to zero by default in c?

0 Answers  


Write a program to generate the Fibinocci Series

0 Answers   TISL,


How to declare pointer variables?

0 Answers  






Write a program to check prime number in c programming?

0 Answers  


What does static mean in c?

1 Answers  


what is the difference between arrays and linked list

26 Answers   MAHINDRA, Tech Mahindra, Wipro,


What does a function declared as pascal do differently?

0 Answers  


How do you write a program which produces its own source code as its output?

4 Answers  


Given an array A[n+m] of n+m numbers, where A[1] ... A[n] is sorted and A[n+1] ... A[n+m] is sorted. Design a linear time algorithm to obtain A[1...n+m] sorted using only O(1) extra space. Time Complexity of your algorithm should be O(n) and Space Complexity O(1).

0 Answers  


what is the output of the below code? main( ) { printf ( "\nOnly stupids use C?" ) ; display( ) ; } display( ) { printf ( "\nFools too use C!" ) ; main( ) ; }

3 Answers  


Categories