• C

implement

how to implement strrev with out using temporary variables and recursion
nagaharikolaAsked:
Who is Participating?
 
evilrixConnect With a Mentor Senior Software Engineer (Avast)Commented:
Well, given that you are going to have to swap values around you generally need a temp variable but there is a way to avoid this.
http://en.wikipedia.org/wiki/XOR_swap_algorithm

Note, you can perform the swap in place... you don't need to swap to another string!

As for the best way of doing this, you only need to swap 1/2 the elements in the string so start at one end and swap with the counter-part at the other end. So, if a string is 5 chars long you only have to swap 0 & 4 and 1 & 3

hfebd
dfebh
dbefh

Does that make sense?
0
 
evilrixSenior Software Engineer (Avast)Commented:
Also, if this is an assignment it would be most helpful to post the exact wording. There are a number of ways this problem could be solved and the actual wording of the assignment will determine which is the best to consider. Once you have clarified these point either I or another expert will be more than happy to guide you through finding an appropriate solution.

Thanks.
0
 
nagaharikolaAuthor Commented:
this is not academic work.
I am practising string operations, i tried with recursion and using temp variables.
I am stuck up while implementing with out recursion and temp variable.


0
Improved Protection from Phishing Attacks

WatchGuard DNSWatch reduces malware infections by detecting and blocking malicious DNS requests, improving your ability to protect employees from phishing attacks. Learn more about our newest service included in Total Security Suite today!

 
nagaharikolaAuthor Commented:
The below is the program i wrote
#include<stdio.h>
main()
{
  char str[100],rev[100];
  char *i= str;
  char *j= rev;
  printf("enter a string\n");
 gets(str);
          while(*i!='\0')
          i++;
      i--;
          while(i>=str)
          *j++=*i--;
*j='\0';
printf("The string entered is %s  \nThe string after reversing is %s\n\n",str,rev);
}



help to implement the above program with out
  char *i= str;
  char *j= rev
0
 
nagaharikolaAuthor Commented:
I've requested that this question be deleted for the following reason:

got the solution
0
 
evilrixSenior Software Engineer (Avast)Commented:
So, what is the solution you used then? Please share it with us. You can then close this by accepting your own solution as the answer.
0
 
evilrixSenior Software Engineer (Avast)Commented:
FWIW; the solution I provided was quite a reasonable way to do this given your constraints. It would have been nice to have gotten some feedback from you. If it didn't solve your problem you could have said and I'd have been glad to work with you to refine it.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.