ALLInterview.com :: Home Page KalAajKal.com
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
info       Did you received any Funny E-Mails from your Friends and like to share with rest of our friends? Yeah!! you can post that stuff   HERE
Google
 
Categories  >>  Code Snippets  >>  Programming Code  >>  C Code
 
 


 

 
 C Code interview questions  C Code Interview Questions
 C++ Code interview questions  C++ Code Interview Questions
 VC++ Code interview questions  VC++ Code Interview Questions
 Java Code interview questions  Java Code Interview Questions
 Dot Net Code interview questions  Dot Net Code Interview Questions
 Visual Basic Code interview questions  Visual Basic Code Interview Questions
 Programming Code AllOther interview questions  Programming Code AllOther Interview Questions
Question
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.
 Question Submitted By :: =-PKG-=
I also faced this Question!!     Rank Answer Posted By  
 
  Re: 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.
Answer
# 1
/*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 ?    0 Yes 0 No
Raghuram
 
  Re: 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.
Answer
# 2
//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
Prabhakar
 
 
 

 
 
 
Other C Code Interview Questions
 
  Question Asked @ Answers
 
main() { int i=5; printf("%d",++i++); }  1
how to check whether a linked list is circular.  3
#include<stdio.h> main() { char s[]={'a','b','c','\n','c','\0'}; char *p,*str,*str1; p=&s[3]; str=p; str1=s; printf("%d",++*p + ++*str1-32); }  1
How do I write a program to print proper subset of given string . Eg :input: abc output:{},{a},{b},{c},{a,b},{a,c},{b,c}, {a,b,c}.I desperately need this program please mail me to saravana6m@gmail.com Deshaw9
main() { int i=10,j=20; j = i, j?(i,j)?i:j:j; printf("%d %d",i,j); }  1
main( ) { char *q; int j; for (j=0; j<3; j++) scanf(“%s” ,(q+j)); for (j=0; j<3; j++) printf(“%c” ,*(q+j)); for (j=0; j<3; j++) printf(“%s” ,(q+j)); }  1
Is the following statement a declaration/definition. Find what does it mean? int (*x)[10];  1
Write a procedure to implement highlight as a blinking operation  1
How do you write a program which produces its own source code as its output?  7
plz send me all data structure related programs  1
main() { int i=0; for(;i++;printf("%d",i)) ; printf("%d",i); }  1
#define DIM( array, type) sizeof(array)/sizeof(type) main() { int arr[10]; printf(“The dimension of the array is %d”, DIM(arr, int)); }  1
int aaa() {printf(“Hi”);} int bbb(){printf(“hello”);} iny ccc(){printf(“bye”);} main() { int ( * ptr[3]) (); ptr[0] = aaa; ptr[1] = bbb; ptr[2] =ccc; ptr[2](); }  1
main ( ) { static char *s[ ] = {“black”, “white”, “yellow”, “violet”}; char **ptr[ ] = {s+3, s+2, s+1, s}, ***p; p = ptr; **++p; printf(“%s”,*--*++p + 3); }  1
main() { int y; scanf("%d",&y); // input given is 2000 if( (y%4==0 && y%100 != 0) || y%100 == 0 ) printf("%d is a leap year"); else printf("%d is not a leap year"); }  1
#include <stdio.h> main() { char * str = "hello"; char * ptr = str; char least = 127; while (*ptr++) least = (*ptr<least ) ?*ptr :least; printf("%d",least); }  1
Program to Delete an element from a doubly linked list. Infosys4
How to swap two variables, without using third variable ? HCL45
main() { int i = 258; int *iPtr = &i; printf("%d %d", *((char*)iPtr), *((char*)iPtr+1) ); }  1
main(){ unsigned int i; for(i=1;i>-2;i--) printf("c aptitude"); }  1
 
For more C Code Interview Questions Click Here 
 
 
 
 
 
   
Copyright Policy  |  Terms of Service  |  Help  |  Site Map 1  |  Articles  |  Site Map  |   Site Map  |  Contact Us interview questions urls   External Links 
   
Copyright © 2007  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  Forum9.com   ::  KalAajKal.com