Given an array of characters which form a sentence of words,
give an efficient algorithm to reverse the order of the words
(not characters) in it.




Answers were Sorted based on User's Feedback



Given an array of characters which form a sentence of words, give an efficient algorithm to reverse..

Answer / raghuram

/*in 2n comparisons*/


#include<iostream.h>
#include<string.h>
#include<stdio.h>

int count=0;
void rev_word(char str[20],int m,int n)
{
int i,l,k;
k=n-m+1;
if(k%2==0)
l=(k/2-1);
else
l=k/2;
k=n;
for(i=m;i<=m+l;i++)
{
char t=str[i];
str[i]=str[k];
str[k]=t;
k--;
}
}
int main()
{
char str[100];
int i,j=0;
cout<<"\n\nenter string:";
gets(str);

rev_word(str,0,strlen(str)-1);


for(i=0;i<=strlen(str);i++)
{
if(str[i]==' '||str[i]=='\0')
{
rev_word(str,j,i-1);
j=i;
while(str[j]==' ')
j++;
i=j;
}
}
cout<<"\n\nsentence with order of words reversed is:";
cout<<str;
return 0;
}


Is This Answer Correct ?    1 Yes 0 No

Given an array of characters which form a sentence of words, give an efficient algorithm to reverse..

Answer / prabhakar

//Simple String Reverse
#include<stdio.h>
#include<string.h>
char str1[50],str2[50];
main()
{
printf("\nEnter The String:");
gets(str1);
rev(str1);
}
rev(char s[20])
{
int i=0,a=0,j=0,k=0;
a=strlen(s);
b:
a--;
while(s[a]!=' '&a>=0)
{
str2[i]=s[a];
a--,i++;
}
str2[i]='\0';
j=strlen(str2)-1;
while(str2[k]!='\0')
{
printf("%c",str2[j]);
j--,k++;
}
printf(" ");
if(a>=0)
{
goto b;
}
printf("\n\n");
}

Is This Answer Correct ?    0 Yes 0 No

Post New Answer




More C Code Interview Questions

Who could write how to find a prime number in dynamic array?

0 Answers  


Print an integer using only putchar. Try doing it without using extra storage.

2 Answers  


Develop a routine to reflect an object about an arbitrarily selected plane

0 Answers  


program to find magic aquare using array

4 Answers   HCL,


void main() { int x,y=2,z; z=(z*=2)+(x=y=z); printf("%d",z); }

4 Answers  






Write a program that reads a dynamic array of 40 integers and displays only even integers

2 Answers  


write a program to Insert in a sorted list

4 Answers   Microsoft,


main( ) { void *vp; char ch = ‘g’, *cp = “goofy”; int j = 20; vp = &ch; printf(“%c”, *(char *)vp); vp = &j; printf(“%d”,*(int *)vp); vp = cp; printf(“%s”,(char *)vp + 3); }

1 Answers  


#include <stdio.h> #define a 10 main() { #define a 50 printf("%d",a); }

2 Answers  


write a c program to Create a registration form application by taking the details like username, address, phone number, email along with password and confirm password (should be same as password).Ensure that the password is of 8 characters with only numbers and alphabets. Take such details for 5 users and display the details. In place of password display “****”. (Use Structures).

0 Answers   CDAC, College School Exams Tests,


main() { char *p="GOOD"; char a[ ]="GOOD"; printf("\n sizeof(p) = %d, sizeof(*p) = %d, strlen(p) = %d", sizeof(p), sizeof(*p), strlen(p)); printf("\n sizeof(a) = %d, strlen(a) = %d", sizeof(a), strlen(a)); }

1 Answers  


Write a Program that Inputs 10 Numbers in an Array and Show the Maximum Number

2 Answers   Ace Info,






Categories