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
 
 


 

 
 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
String reverse with time complexity of n/2 with out using
temporary variable.
 Question Submitted By :: Guest
I also faced this Question!!     Rank Answer Posted By  
 
  Re: String reverse with time complexity of n/2 with out using temporary variable.
Answer
# 1
#include<iostream.h>

#include<string.h>//complexity-n/2

int main()
{
int i,l,l1;
char str[100];
cout<<"enter string:";
gets(str);
l=strlen(str);
if(l%2==0)
l1=(l/2-1);
else
l1=l/2;
for(i=0;i<=l1;i++)/*swap elements from 2 ends till u reach
middle part of array*/
{
char t=str[i];
str[i]=str[l-i-1];
str[l-i-1]=t;
}
str[l]=0;
cout<<"\n\nreversed string is:"<<str;
getch();
return 0;
}
 
Is This Answer Correct ?    2 Yes 6 No
Raghuram
 
  Re: String reverse with time complexity of n/2 with out using temporary variable.
Answer
# 2
#include<stdio.h>
#include<string.h>
main(int argc, char *argv[])
{
  char  *string = argv[1];
  int len = strlen(string);
  int i = 0;
  int j = len - 1;
  printf("string before is %s\n", string);
  printf("len is %d\n", len);
  while(i <= j)
  {
    *(string+i) += *(string+j);
    *(string+j) = *(string+i) - *(string+j);
    *(string+i) = *(string+i) - *(string+j);
    i++;
    j--;
    if(len % 2)
     if(i == j) break;
  }
  printf("reversed string is %s\n", string);
}
 
Is This Answer Correct ?    6 Yes 1 No
Gayathri Sundar
 
 
 
  Re: String reverse with time complexity of n/2 with out using temporary variable.
Answer
# 3
#include<stdio.h>

void reverse(char *);

void main()
{
	char str[]="Hello";

	reverse(str);
	printf("Reverse String is %s",str);
	
}

void reverse(char *p)
{

	char *q=p;
	while(*++q!='\0');
	q--;

	while(p<q)
	{
		*p=*p+*q;
		*q=*p-*q;
		*p=*p-*q;
		p++;
		q--;
	}

}
 
Is This Answer Correct ?    3 Yes 1 No
Atul Kabra
 
  Re: String reverse with time complexity of n/2 with out using temporary variable.
Answer
# 4
#inclue<stdio.h>
       #include<string.h>
       main(){
           char a[10],i;
           int len=1;
           printf(" Enter string ");
           fgets(a,9,stdin);
     
           len = strlen(a);
     
      
          for(i=0 ; i<(len/2) ; i++){
                  a[i]=a[i]+a[len-2];
                  a[len-2]=a[i]-a[len-2];
                  a[i]=a[i]-a[len-2];
                  len--;
          }
          printf("\n Reverse string with n/2 complexity
 %s",a);
          return 0;
      }
 
Is This Answer Correct ?    1 Yes 0 No
Suraj Bhan Gupta
 
  Re: String reverse with time complexity of n/2 with out using temporary variable.
Answer
# 5
refer c book!
    or
contact me
 
Is This Answer Correct ?    0 Yes 3 No
Prof.muthu (9962940220)
 
  Re: String reverse with time complexity of n/2 with out using temporary variable.
Answer
# 6
It's all O(n). You're finding the length of the string,
which itself is an O(n) operation.
So, O(n + n/2) = O(n).
 
Is This Answer Correct ?    0 Yes 1 No
A
 
  Re: String reverse with time complexity of n/2 with out using temporary variable.
Answer
# 7
#include <iostream>
#include <string>
using std::string;
using std::cout;
using std::endl;
using std::cin;

int main()
{
    string s("abcdefghijklmnopqrstuvwxyz");
    string::size_type s_size = s.size();

    for (string::size_type x = 0; x != s_size; x++){

        if ( (s_size -x -1) > x ){

            s[x] ^= s[s_size - x -1];
            s[s_size - x -1] ^= s[x];
            s[x] ^= s[s_size - x -1];

        }else{
            break;
        }

    }

    cout << s << endl;
}
 
Is This Answer Correct ?    1 Yes 0 No
Mm Chen
 
  Re: String reverse with time complexity of n/2 with out using temporary variable.
Answer
# 8
int i=0,len=strlen(str);
int j=len/2;len--;
while(i<j)
{
*(str+i)^=*(str+len)^=*(str+i)^=*(str+len);
len--;i++;
}
 
Is This Answer Correct ?    1 Yes 0 No
Anurag
 

 
 
 
Other C Code Interview Questions
 
  Question Asked @ Answers
 
how to delete an element in an array  1
#include<stdio.h> main() { int i=1,j=2; switch(i) { case 1: printf("GOOD"); break; case j: printf("BAD"); break; } }  1
1) int i=5; j=i++ + i++ + i++; printf("%d",j);This code gives the answer 15.But if we replace the value of the j then anser is different?why? 2)int i=5; printf("%d",i++ + i++ + i++); this givs 18. Infosys6
main() { int i, j, *p; i = 25; j = 100; p = &i; // Address of i is assigned to pointer p printf("%f", i/(*p) ); // i is divided by pointer p } a. Runtime error. b. 1.00000 c. Compile error d. 0.00000 HCL1
main() { int i=400,j=300; printf("%d..%d"); }  1
Give a oneline C expression to test whether a number is a power of 2? Motorola16
Write a program that find and print how many odd numbers in a binary tree  1
1 o 1 1 0 1 0 1 0 1 1 0 1 0 1 how to design this function format in c-language ?  1
struct Foo { char *pName; char *pAddress; }; main() { struct Foo *obj = malloc(sizeof(struct Foo)); clrscr(); obj->pName = malloc(100); obj->pAddress = malloc(100); strcpy(obj->pName,"Your Name"); strcpy(obj->pAddress, "Your Address"); free(obj); printf("%s", obj->pName); printf("%s", obj->pAddress); } a. Your Name, Your Address b. Your Address, Your Address c. Your Name Your Name d. None of the above HCL1
main() { int c[ ]={2.8,3.4,4,6.7,5}; int j,*p=c,*q=c; for(j=0;j<5;j++) { printf(" %d ",*c); ++q; } for(j=0;j<5;j++){ printf(" %d ",*p); ++p; } }  1
main() { { unsigned int bit=256; printf("%d", bit); } { unsigned int bit=512; printf("%d", bit); } } a. 256, 256 b. 512, 512 c. 256, 512 d. Compile error HCL1
programming in c lanugaue programm will errror error with two header file one as stdio.h and other one is conio.h  1
main() { int i=300; char *ptr = &i; *++ptr=2; printf("%d",i); }  1
Extend the sutherland-hodgman clipping algorithm to clip three-dimensional planes against a regular paralleiepiped IBM1
int i,j; for(i=0;i<=10;i++) { j+=5; assert(i<5); }  1
main() { extern i; printf("%d\n",i); { int i=20; printf("%d\n",i); } }  1
why java is platform independent? Wipro10
Write a C function to search a number in the given list of numbers. donot use printf and scanf Honeywell4
main() { int i=5,j=10; i=i&=j&&10; printf("%d %d",i,j); }  1
#define DIM( array, type) sizeof(array)/sizeof(type) main() { int arr[10]; printf(“The dimension of the array is %d”, DIM(arr, int)); }  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