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
 
 


 

 
 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 :: =-PKG-=
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
/*in 2n comparisons*/


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

int count=0;
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++)
{ 
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++)
{   
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 0 No
Raghuram
 
  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
//Simple String Reverse
#include<stdio.h>
#include<string.h>
char str1[50],str2[50];
main()
{
        printf("\nEnter The String:");
        gets(str1);
        rev(str1);
}
rev(char s[20])
{
        int i=0,a=0,j=0,k=0;
        a=strlen(s);
        b:
        a--;
        while(s[a]!=' '&a>=0)
        {
                str2[i]=s[a];
                a--,i++;
        }
        str2[i]='\0';
        j=strlen(str2)-1;
        while(str2[k]!='\0')
        {
                printf("%c",str2[j]);
                j--,k++;
        }
        printf(" ");
        if(a>=0)
        {
                goto b;
        }
        printf("\n\n");
}
 
Is This Answer Correct ?    0 Yes 0 No
Prabhakar
 
 
 

 
 
 
Other C Code Interview Questions
 
  Question Asked @ Answers
 
main() { char a[4]="HELL"; printf("%s",a); } Wipro1
Find your day from your DOB? Microsoft12
main() { int i = 100; clrscr(); printf("%d", sizeof(sizeof(i))); } a. 2 b. 100 c. 4 d. none of the above HCL2
int i,j; for(i=0;i<=10;i++) { j+=5; assert(i<5); }  1
Print an integer using only putchar. Try doing it without using extra storage.  1
Write out a function that prints out all the permutations of a string. For example, abc would give you abc, acb, bac, bca, cab, cba. You can assume that all the characters will be unique. Microsoft4
main( ) { char *q; int j; for (j=0; j<3; j++) scanf(“%s” ,(q+j)); for (j=0; j<3; j++) printf(“%c” ,*(q+j)); for (j=0; j<3; j++) printf(“%s” ,(q+j)); }  1
Give a one-line C expression to test whether a number is a power of 2. Microsoft8
program to find magic aquare using array HCL3
void main() { int i=5; printf("%d",i+++++i); }  1
main() { int i=300; char *ptr = &i; *++ptr=2; printf("%d",i); }  1
Is this code legal? int *ptr; ptr = (int *) 0x400;  1
main() { int i=5; printf("%d%d%d%d%d%d",i++,i--,++i,--i,i); }  1
How do you sort a Linked List (singly connected) in O(n) please mail to pawan.10k@gmail.com if u can find an anser...i m desperate to knw... Oracle3
Write a prog to accept a given string in any order and flash error if any of the character is different. For example : If abc is the input then abc, bca, cba, cab bac are acceptable, but aac or bcd are unacceptable. Microsoft5
main(){ unsigned int i; for(i=1;i>-2;i--) printf("c aptitude"); }  1
What is the problem with the following code segment? while ((fgets(receiving array,50,file_ptr)) != EOF) ;  1
What is the output for the following program main() { int arr2D[3][3]; printf("%d\n", ((arr2D==* arr2D)&&(* arr2D == arr2D[0])) ); }  1
program to Reverse a linked list Ness-Technologies4
main() { int i=0; while(+(+i--)!=0) i-=i++; printf("%d",i); }  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