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   SiteMap shows list of All Categories in this site.
Google
 
Categories  >>  Software  >>  Programming Languages  >>  C
 
 


 

 
 C interview questions  C Interview Questions
 C++ interview questions  C++ Interview Questions
 VC++ interview questions  VC++ Interview Questions
 Delphi interview questions  Delphi Interview Questions
 Programming Languages AllOther interview questions  Programming Languages 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 :: San
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
#include<stdio.h>
int main()
{
char *p="i am working in TechM";
char *s,*temp;
char a[20];
int i=0;
s=p;
while(*p != '\0')
p++;
while(s != p){
while(*(--p)!=' ');
temp=p;
p++;
while(*p != '\0' && *p != ' ')
{
a[i++]=*p;
p++;
}
a[i++]=' ';
p=temp;
p--;
}
while(*s != ' ')
a[i++]=*s++;
a[i] = '\0';
printf("%s \n",a);
}
~
 
Is This Answer Correct ?    2 Yes 0 No
Tarak
 
  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
#include <stdio.h>

void reverse_string ( char* cp_string, int start, int end )
{
	if ( cp_string && ( start < end ) ) {
		*( cp_string + start ) ^= *( cp_string + 
end  ) 
			^=  *( cp_string + start ) ^= *( 
cp_string + end );
		reverse_string ( cp_string, ++start, --
end ); 
	}
}


int main ( int argc, char* argv [] )
{
	char ca_array [12]={"Hello World"};
	char* cp_ptr = NULL;
	char* cp_ptr_temp = NULL;
	
	printf ( "\n Before Reverse :%s", ca_array );

	reverse_string ( ca_array, 0, strlen ( ca_array ) -
1 );

	cp_ptr_temp = cp_ptr = ca_array;
	while ( NULL != ( cp_ptr_temp = (char*) strchr ( 
cp_ptr_temp, ' ' ) ) ) {
		reverse_string ( cp_ptr, 0, ( cp_ptr_temp - 
cp_ptr ) - 1 );
		cp_ptr = ++cp_ptr_temp;
	}
	// change the final word
	reverse_string ( cp_ptr, 0, 
			( ( strlen ( ca_array ) - 1 ) - ( 
cp_ptr - ca_array ) ) );

	printf ( "\n After Reverse by Words :%s", 
ca_array );
	return ( 0 );
}
 
Is This Answer Correct ?    1 Yes 0 No
Abdur Rab
 
 
 
  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
# 3
#include <stdafx.h>
#include <string.h>

char* ReverseString(char *a){

 int length = strlen(a);
 printf("length is:%d\n",length);
 int i=0;
 int j=length-1;
 while(i<j){
	 *(a+i)=*(a+i)^*(a+j);
	 *(a+j)=*(a+i)^*(a+j);
	 *(a+i)=*(a+i)^*(a+j);
	 i++;
	 j--;
 }
 
 return a;
}

int main()
{
 char s[] = "katrina kaif is gorgeous gal!"; 
 char *a=s;
 char *b=s;
 char *e= a+strlen(a);
 printf("String is:%s\n",a);
 ReverseString(a);
 printf("NewString is:%s\n",a);
 int i=0;
 while(*(a+i)!='\0'){
	 while(*(a+i)!=' ' && *(a+i)!='\0') i++;
		*(a+i)='\0';
		ReverseString(a);
		
		if((a+i)!=e){
			*(a+i)=' ';
			a = a+i+1;
		}
		else{
			break;
		}
		i=0;
 }
 printf("FinalString is:%s\n",b);
 getchar();
 
Is This Answer Correct ?    1 Yes 0 No
Anubhav Meena
 

 
 
 
Other C Interview Questions
 
  Question Asked @ Answers
 
class foo { public: static int func(const char*& p) const; }; This is illegal, why? Google6
what is a function pointer and how all to declare ,define and implement it ??? Honeywell3
why i join syntel? Syntel12
Add 2 64 bit numbers on a 32 bit machine NetApp3
how can i make a program with this kind of output.. Enter a number: 5 0 01 012 0123 01234 012345 01234 0123 012 01 0 Wipro3
pgm to find middle element of linklist(in efficent manner) Huawei2
which operator having highest precedence? a.)+ b.)++ c.)= d.)%  3
what is the difference between entry control and exit control statement?  2
what is difference b/w extern & volatile variable?? Teleca2
write a programme that inputs a number by user and gives its multiplication table.  2
write a c programs to do multiplication of two numbers with out using arithmatic operator ?????????? TCS4
What is the difference between null pointer and the void pointer?  2
which types of data structure will i use to convert infix to post fix??? IIT3
#include <stdio.h> #define sqr(x) (x*x) int main() { int x=2; printf("value of x=%d",sqr(x+1)); } What is the value of x? Oracle13
why division operator not work in case of float constant?  1
what is the defrenece between structure and union  5
Why doesn't the code "a[i] = i++;" work?  4
program for validity of triangle from 3 side  6
without using arithmatic operator solve which number is greater??????????  1
write a program to sort the elements in a given array in c language  2
 
For more C 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