String reverse with time complexity of n/2 with out using
temporary variable.

Answers were Sorted based on User's Feedback



String reverse with time complexity of n/2 with out using temporary variable...

Answer / gayathri sundar

#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 ?    9 Yes 3 No

String reverse with time complexity of n/2 with out using temporary variable...

Answer / manish pathak

//********this is perfect answer***************

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n,num,rem,l;
char s[]="abcde";
l=sizeof(s)-1;
for(i=0,j=l-1;i<=(l-1)/2;i++)//save space n/2
{

s[i]=s[i]+s[j-i];
s[j-i]=s[i]-s[j-i];
s[i]=s[i]-s[j-i];

}

s[l]='\0';
printf("%s",s);
getch();
}

Is This Answer Correct ?    5 Yes 1 No

String reverse with time complexity of n/2 with out using temporary variable...

Answer / a

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 ?    4 Yes 1 No

String reverse with time complexity of n/2 with out using temporary variable...

Answer / mm chen

#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 ?    3 Yes 0 No

String reverse with time complexity of n/2 with out using temporary variable...

Answer / anurag

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 ?    3 Yes 1 No

String reverse with time complexity of n/2 with out using temporary variable...

Answer / atul kabra

#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 3 No

String reverse with time complexity of n/2 with out using temporary variable...

Answer / suraj bhan gupta

#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 ?    2 Yes 2 No

String reverse with time complexity of n/2 with out using temporary variable...

Answer / snehal

#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-1];
a[len-1]=a[i]-a[len-1];
a[i]=a[i]-a[len-1];
len--;
}
printf("\n Reverse string with n/2 complexity
%s",a);
return 0;
}

Is This Answer Correct ?    1 Yes 1 No

String reverse with time complexity of n/2 with out using temporary variable...

Answer / raghuram

#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 11 No

String reverse with time complexity of n/2 with out using temporary variable...

Answer / prof.muthu (9962940220)

refer c book!
or
contact me

Is This Answer Correct ?    0 Yes 13 No

Post New Answer

More C Code Interview Questions

Is the following code legal? typedef struct a aType; struct a { int x; aType *b; };

1 Answers  


Find the largest number in a binary tree

7 Answers   Infosys,


int aaa() {printf(“Hi”);} int bbb(){printf(“hello”);} iny ccc(){printf(“bye”);} main() { int ( * ptr[3]) (); ptr[0] = aaa; ptr[1] = bbb; ptr[2] =ccc; ptr[2](); }

1 Answers  


Write a program to check whether the number is prime and also check if it there i n fibonacci series, then return true otherwise return false

1 Answers   Cognizant, lenovo,


int i,j; for(i=0;i<=10;i++) { j+=5; assert(i<5); }

3 Answers   Cisco, HCL,






main() { int i; clrscr(); printf("%d", &i)+1; scanf("%d", i)-1; } a. Runtime error. b. Runtime error. Access violation. c. Compile error. Illegal syntax d. None of the above

1 Answers   HCL,


#include<stdio.h> main() { int a[2][2][2] = { {10,2,3,4}, {5,6,7,8} }; int *p,*q; p=&a[2][2][2]; *q=***a; printf("%d..%d",*p,*q); }

1 Answers  


what is the output of the below program & why ? #include<stdio.h> void main() { int a=10,b=20,c=30; printf("%d",scanf("%d%d%d",&a,&b,&c)); }

6 Answers   CSC, IIIT,


Given n nodes. Find the number of different structural binary trees that can be formed using the nodes.

16 Answers   Aricent, Cisco, Directi, Qualcomm,


#include<stdio.h> main() { char s[]={'a','b','c','\n','c','\0'}; char *p,*str,*str1; p=&s[3]; str=p; str1=s; printf("%d",++*p + ++*str1-32); }

2 Answers   CNSI,


main() { struct student { char name[30]; struct date dob; }stud; struct date { int day,month,year; }; scanf("%s%d%d%d", stud.rollno, &student.dob.day, &student.dob.month, &student.dob.year); }

1 Answers  


# include<stdio.h> aaa() { printf("hi"); } bbb(){ printf("hello"); } ccc(){ printf("bye"); } main() { int (*ptr[3])(); ptr[0]=aaa; ptr[1]=bbb; ptr[2]=ccc; ptr[2](); }

1 Answers  


Categories