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                      
tip       Ask Questions on ANYTHING, that arise in your Daily Life at     FORUM9.COM
Google
 
Categories >> Code-Snippets >> Programming-Code >> C-Code
 
 
 
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 :: Maggy
I also faced this Question!!     Rank Answer Posted By  
 
Answer
//check this program....

#include <string.h>
#include <stdio.h>
main(){
        char a[100],b[100];
        int i,j=0,c=0,k,cnt=0;
        printf("enter the sentence\n");
        for(i=0;i>=0;i++){
         a[i]=getchar();
         if(a[i]=='\n')break;
        }
        for(i=0;a[i]!='\n';i++)
          cnt++;
        for(i=0;i<(cnt+1);i++)
          b[i]=' ';
        for(i=0;i<(cnt+1);i++){
            c++;
          if((a[i]==' ')||(a[i]=='\n')){
            c=c-1;
            for(j=(cnt-i),k=(i-c);c>0;k++,j++,c--){
              b[j]=a[k];
            }c=0;
           }
         }
              printf("result: ");
              for(i=0;i<cnt+1;i++)
              putchar(b[i]); printf("\n");
 }
 
0
Patrck
 
View All Answers
 
 
 
 
 
   
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