String reverse with time complexity of n/2 with out using
temporary variable.
Answers were Sorted based on User's Feedback
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
#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 |
why java is platform independent?
which function is used to clear the buffer stream on gcc? for example: I wrote following code on gcc #include<stdio.h> int main(void) { char ch; int a,b; printf("\nenter two numbers:\t"); scanf("%d%d",&a,&b); printf("enter number is %d and %d",a,b); printf("\nentercharacter:\t"); scanf("%c",&ch); printf("enter character is %c",ch); return 0; } in above progarm ch could not be scan. why?plz tell me solution.
programming in c lanugaue programm will errror error with two header file one as stdio.h and other one is conio.h
int i=10; main() { extern int i; { int i=20; { const volatile unsigned i=30; printf("%d",i); } printf("%d",i); } printf("%d",i); }
main() { if ((1||0) && (0||1)) { printf("OK I am done."); } else { printf("OK I am gone."); } } a. OK I am done b. OK I am gone c. compile error d. none of the above
main() { unsigned int i=65000; while(i++!=0); printf("%d",i); }
why the range of an unsigned integer is double almost than the signed integer.
write a c program to input initial & final time in the format hh:mm and find the time intervel between them? Ex inputs are initial 06:30 final 00:05 and 23:22 final 22.30
Set up procedure for generating a wire frame display of a polyhedron with the hidden edges of the object drawn with dashed lines
1. const char *a; 2. char* const a; 3. char const *a; -Differentiate the above declarations.
#define square(x) x*x main() { int i; i = 64/square(4); printf("%d",i); }
4 Answers Google, HCL, Quick Heal, WTF,
Write a Program that Inputs 10 Numbers in an Array and Show the Maximum Number