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                      
Do you have a collection of Interview Questions and interested to share with us!!
Please send that collection to along with your userid / name. ThanQ
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<stdio.h>
main()
{
	int i=0,j=0,start=0,end=0,len,w_len;
	char temp;
	char str[]="Papa Kehte HAIN bada naam karega";
	printf("Before reversing the words string is %s \n",str);
	len=strlen(str);
	for(i=0,j=len-1;i<j;i++,j--)
	{
		temp=str[j];
		str[j]=str[i];
		str[i]=temp;
	}
	for(i=0,j=0;str[i]!=0;)
	{
		i=j;
		for(;str[i]!=' '&& i<len;)
			i++;
		w_len=(i-j)-1;
		
		for(start=j,end=(start+w_len);start<end;start++,end--)
		{
			temp=str[start];
			str[start]=str[end];
			str[end]=temp;
		}
		j=i+1;
	}
	printf("After  reversing the words string is %s \n",str);
}
 
0
Vijay
 
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