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  >>  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 :: Maggy
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"
#include"stdlib.h"

int reverse(char *string, char delimiter)
{
  char *src, *dest;
  char *temp = string;

  while( *temp )
  {
    if (*temp == delimiter)
    {
      temp++;
      continue;
    }

    src=dest=temp;
    while ( (*(dest+1) != delimiter) &&
            ( *(dest+1) != '\0' )) dest++;

            //( *(dest+1) != '\n' ) &&
    temp=dest+1;
    while( dest > src )
    {
      char tmp = *dest;
      //*dest -- = *src;
      *dest = *dest-- *src;
      //*src++=tmp;
      *src = *src++ tmp;
    }
  }
  return 0;
}

int main()
{
    char name[] = "vinod kumar dhochak";
    printf("%s\n",name);
    reverse(name,' '); /* space as delimiter,Reverse Words 
*/
    printf("%s\n",name);
    reverse(name,'\n'); /* Reverse Complete Sentence */
    printf("%s\n",name);
}
 
Is This Answer Correct ?    2 Yes 2 No
Vinod Kumar
 
  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<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
 
Is This Answer Correct ?    0 Yes 2 No
Rahul Shandilya
 
 
 
  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
/*complexity-2n..check d prog.  */

#include<iostream.h>
#include<string.h>
#include<stdio.h>


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++)
{ count++;
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++)
{    count++;
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 3 No
Raghuram.A
 
  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
# 4
#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);
}
 
Is This Answer Correct ?    2 Yes 1 No
Vijay
 
  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
# 5
this works in O(n) as first reverse all the sentence....then
reverse each word....


#include<iostream.h>
#include<conio.h>
#include<string.h>

char a[50];

void wrd_reverse()
{
void str_rev(int start_index,int len);
int t=strlen(a);
str_rev(0,t);
int i=0;int st_indx=0;int count=0;
int size;
while(a[i]!='\0')
	{
	if(a[i]!=' ')
	count++;
	else
	{

	size=count;
	str_rev(st_indx,size);
	count=0;
	st_indx=i+1;
	}
	i++;
	}
size=count;
str_rev(st_indx,size);

}

void str_rev(int start_index,int len)
{
int sft=start_index;
int lenm=len/2;
len--;
for(int i=0;i<lenm;i++)
	{
	a[sft+i]=a[sft+i]+a[(sft+len)-i];
	a[(sft+len)-i]=a[sft+i]-a[(sft+len)-i];
	a[sft+i]=a[sft+i]-a[(sft+len)-i];
	}
}

int main(){
cin.getline(a,40);
void wrd_reverse();
wrd_reverse();
cout.write(a,40);
return 0;

}
 
Is This Answer Correct ?    0 Yes 0 No
Ashish
 
  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
# 6
//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");
 }
 
Is This Answer Correct ?    3 Yes 0 No
Patrck
 

 
 
 
Other C Code Interview Questions
 
  Question Asked @ Answers
 
main() { extern int i; i=20; printf("%d",i); }  1
Extend the sutherland-hodgman clipping algorithm to clip three-dimensional planes against a regular paralleiepiped IBM1
#include<conio.h> main() { int x,y=2,z,a; if(x=y%2) z=2; a=2; printf("%d %d ",z,x); }  1
main() { unsigned int i=10; while(i-->=0) printf("%u ",i); }  1
void ( * abc( int, void ( *def) () ) ) ();  1
Finding a number which was log of base 2 NetApp1
main() { int a[10]; printf("%d",*a+1-*a+3); }  1
Is this code legal? int *ptr; ptr = (int *) 0x400;  1
Is the following code legal? struct a { int x; struct a *b; }  1
Write a program that find and print how many odd numbers in a binary tree  1
Given n nodes. Find the number of different structural binary trees that can be formed using the nodes. Aricent7
main() { char p[ ]="%d\n"; p[1] = 'c'; printf(p,65); }  1
main() { char *p; p="Hello"; printf("%c\n",*&*p); }  1
what will be the position of the file marker? a: fseek(ptr,0,SEEK_SET); b: fseek(ptr,0,SEEK_CUR);  1
#define prod(a,b) a*b main() { int x=3,y=4; printf("%d",prod(x+2,y-1)); }  1
void main() { printf(“sizeof (void *) = %d \n“, sizeof( void *)); printf(“sizeof (int *) = %d \n”, sizeof(int *)); printf(“sizeof (double *) = %d \n”, sizeof(double *)); printf(“sizeof(struct unknown *) = %d \n”, sizeof(struct unknown *)); }  1
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. Microsoft6
Write a routine to draw a circle (x ** 2 + y ** 2 = r ** 2) without making use of any floating point computations at all. Microsoft2
main( ) { int a[2][3][2] = {{{2,4},{7,8},{3,4}},{{2,2},{2,3},{3,4}}}; printf(“%u %u %u %d \n”,a,*a,**a,***a); printf(“%u %u %u %d \n”,a+1,*a+1,**a+1,***a+1); }  1
void main() { int i=i++,j=j++,k=k++; printf(“%d%d%d”,i,j,k); }  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