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
 
 
 
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
#include<iostream>
#include<vector>
using namespace std;
int main()
{
    char str[]="rahul shandilya is going";
    
    string ans;
    int n=0;
    while(str[n]!='\0')
         n++;   
         //cout<<n;  
        bool flag=true;     
   for(int i=n-1;i>=0;i--)
   {
          if(str[i]==' ')
          {
                       int m=i+1;
                      // cout<<m<<" ";
                       string temp;
                       while(str[m]!=' ' && m<n)
                       { 
                                       temp+=str[m];
                                       m++;
                       }
                       //cout<<m<<" ";
                       if(flag)
                       {
                               ans+=temp;
                               flag=false;
                               continue;
                       }
                       if(flag==false)
                       {
                                      ans+=' ';
                                      ans+=temp;
                       }
          } 
   }
    
   cout<<ans; 
    system("pause");
    return 0;
}
//tell me if there is a batter way to do it,i dont think my 
//solution is efficent
 
0
Rahul Shandilya
 
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