Advertisement

06.01.2004 at 01:06AM PDT, ID: 21008644
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

Reverse a string without using temporary variable
Tags: string, reverse, using, without
Hi,

Is there any way I can reverse a string without using a temporary variable ?

Thanks,
Parag.
Start your free trial to view this solution
Question Stats
Zone: Programming
Question Asked By: paragm78
Solution Provided By: Computer101
Participating Experts: 11
Solution Grade: A
Views: 397
Translate:
Loading Advertisement...
06.01.2004 at 01:16AM PDT, ID: 11200986

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.01.2004 at 01:18AM PDT, ID: 11200989

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.01.2004 at 01:18AM PDT, ID: 11200990

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.01.2004 at 01:21AM PDT, ID: 11201002

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.01.2004 at 07:35AM PDT, ID: 11203130

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.01.2004 at 07:42AM PDT, ID: 11203193

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.01.2004 at 07:44AM PDT, ID: 11203211

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.01.2004 at 07:44AM PDT, ID: 11203212

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.01.2004 at 08:17AM PDT, ID: 11203528

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.02.2004 at 12:08AM PDT, ID: 11209904

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.02.2004 at 02:09AM PDT, ID: 11210430

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.02.2004 at 02:26AM PDT, ID: 11210506

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.03.2004 at 02:02AM PDT, ID: 11220309

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.10.2004 at 05:48AM PDT, ID: 11278756

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.10.2004 at 08:31AM PDT, ID: 11280421

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.13.2004 at 04:01AM PDT, ID: 11299369

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.13.2004 at 12:58PM PDT, ID: 11301006

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.20.2004 at 07:43AM PDT, ID: 11354431

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.21.2004 at 01:47AM PDT, ID: 11357780

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
06.22.2004 at 12:00AM PDT, ID: 11366694

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
09.02.2004 at 10:42PM PDT, ID: 11970430

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
09.09.2004 at 12:16PM PDT, ID: 12020286

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
09.13.2004 at 06:08PM PDT, ID: 12050355

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
06.01.2004 at 01:16AM PDT, ID: 11200986

Rank: Master

if you know how to swap two integers and hence characters w/o using temp variable .. then its straight forward..
here is how u can swap two integers

int a,b;
a=45;
b=54;

b=a+b;
a=b-a;
b=b-a;


one more a 'bit' cryptic way to swap is
a ^= b;
b ^= a;
a ^= b;

well .. u got it or need more help?

 
06.01.2004 at 01:18AM PDT, ID: 11200989

Rank: Guru

Parag,

Its possible to 'hide' the temporary variable in recursion or an assignment within an expression or using 'xor' but the temporary variable will still exist at some time or another in some guise.

Here's the classic one but there are others:

void Reverse ( char * str )
{
 int i, j;

 for ( i = 0, j = strlen(str); i < len; i++, j-- )
 {
   // Using XOR:
   str[i] ^= str[j];
   // The temorary variable is actually in the '0' bits of str[j] and the '1' bits of str[i].
   str[j] ^= str[i];
 }
}

 
06.01.2004 at 01:18AM PDT, ID: 11200990
What have you tried in the meantime? Suggested solution:
- make a function that accepts a string (char *s) as parameter
- you need a local variable (e.g. p) here, type char *
- you also need a temporary char variable (unfortunately)
- set p to s + length(s)-1, it then points to the last position
- then swap *s and *p, increment s and decrement p, until s>=p

What temp. variable you don't want? How to swap chars without one?
 
06.01.2004 at 01:21AM PDT, ID: 11201002

Rank: Guru

Parag,

Akshay is right. I missed an ^= but I hope you got the essence of it which is that the temporary variable is still there, its just 'hidden' by various techniques.

Paul.
 
06.01.2004 at 07:35AM PDT, ID: 11203130

Rank: Master

You can use strrev() function.

#include <string.h>

char data[]="Hello World!";
strrev(data);

strrev is not part of the C standard, and it's a C-Extension function which is supported by many compilers.
 
06.01.2004 at 07:42AM PDT, ID: 11203193

Rank: Guru

I just noticed another problem with my code sample above (I should stop when i >= j too). Here it is corrected.

void Reverse ( char * str )
{
 int i, j;

 for ( i = 0, j = strlen(str); i < j; i++, j-- )
 {
   // Using XOR:  The temorary variable is actually in the '0' bits of str[j] and the '1' bits of str[i].
   str[i] ^= str[j];
   str[j] ^= str[i];
   str[i] ^= str[j];
 }
}

This still uses a temporary variable! You just dont notice it because its stored in the '0' bits of the two bytes you are swapping.

Paul
 
06.01.2004 at 07:44AM PDT, ID: 11203211

Rank: Guru

heterodoxe method:
all C strings terminates with 0
you can use this position as your "temporary variable"
Obviously you have to put back the 0 again in the same position.
 
06.01.2004 at 07:44AM PDT, ID: 11203212

Rank: Guru

Actually, j should start at strlen(str) - 1. Sorry! :(
 
06.01.2004 at 08:17AM PDT, ID: 11203528

Rank: Guru

>Actually, j should start at strlen(str) - 1. Sorry! :(
Who imposed this limitation? Maybe my alternative is not the best for paragm78, but it is totally possible.


 
06.02.2004 at 12:08AM PDT, ID: 11209904
Hi all!
Hi  paragm78!
i can suggest the solution using single statement..

try this , it is tested..

x^=y^=x^=y;

u can find more on this at http://www.codeproject.com/tips/stupidxortrick.asp

let me know your comments
bye
vasureddym
 
06.02.2004 at 02:09AM PDT, ID: 11210430

Rank: Master

x^=y^=x^=y;
this is equivalent of
three step XOR earlier mentioned..
just that all three XOR steps are condensed in single..
otherwise both are equally effiecient/inefficient.
 
06.02.2004 at 02:26AM PDT, ID: 11210506

Rank: Guru

My point still stands. It is impossible to swap two values of an array without temporary values. All the above solutions, including mine, use temporary variables in some form.

Before anyone starts shouting, the triple xor method DOES use a temporary variable, it's just hidden in the two real variables. Thats why you need three operations instead of one.

Paul
 
06.03.2004 at 02:02AM PDT, ID: 11220309
Hi,

I guess u can do it without using temp variable.
Try to get the strlen() of the string.
Find out the middle character position
Use to counters : one for the array index from start and other for the array index from end.
In the for loop swap the characters from the first to last position.
Increment first counter and decrement second counter.

I hope this will work.


--Ajay
 
06.10.2004 at 05:48AM PDT, ID: 11278756
Why do I hate these silly assignments? Why do academic programmers make me mad? Oops sorry, went a bit OT there. Here's the answer:

No. Without using a temporary variable, you cannot hold the length of the string, without which you have nothing to swap the first character with. Without using a temporary variable, you cannot even access the array's contents.

And here's the mocking answer since I'm still mad:

No. Use permanent variables instead.
 
06.10.2004 at 08:31AM PDT, ID: 11280421
My statement: every variable is temporary ;)

Which makes it more a NO.
 
06.13.2004 at 04:01AM PDT, ID: 11299369
It is possible, it just looks a little stupid:

switch( strlen( str ) )
{
    case 0: case 1:
        break;
    case 2:
        str[ 0 ] ^= str[ 1 ] ^= str[ 0 ] ^= str[ 1 ];
        break;
    case 3:
        str[ 0 ] ^= str[ 2 ] ^= str[ 0 ] ^= str[ 2 ];
        break;
    case 4:
        str[ 0 ] ^= str[ 3 ] ^= str[ 0 ] ^= str[ 3 ];
        str[ 1 ] ^= str[ 2 ] ^= str[ 1 ] ^= str[ 2 ];
        break;
    case 5:
        str[ 0 ] ^= str[ 4 ] ^= str[ 0 ] ^= str[ 4 ];
        str[ 1 ] ^= str[ 3 ] ^= str[ 1 ] ^= str[ 3 ];
        break;
    // ... implement all other cases up to your maximum string length
}


Or if you think strlen might use temporary variables you can do it like this:

if( str[ 0 ] && str[ 1 ] )
{
    if( !str[ 2 ] )
        str[ 0 ] ^= str[ 1 ] ^= str[ 0 ] ^= str[ 1 ];
    else if( !str[ 3 ] )
        str[ 0 ] ^= str[ 2 ] ^= str[ 0 ] ^= str[ 2 ];
    else if( !str[ 4 ] )
    {
        str[ 0 ] ^= str[ 3 ] ^= str[ 0 ] ^= str[ 3 ];
        str[ 1 ] ^= str[ 2 ] ^= str[ 1 ] ^= str[ 2 ];
    }
    else if( !str[ 5 ] )
    {
        str[ 0 ] ^= str[ 4 ] ^= str[ 0 ] ^= str[ 4 ];
        str[ 1 ] ^= str[ 3 ] ^= str[ 1 ] ^= str[ 3 ];
    }
    // ... implement all other cases up to your maximum str length
}
 
06.13.2004 at 12:58PM PDT, ID: 11301006

Rank: Guru

Where is the author?
 
06.20.2004 at 07:43AM PDT, ID: 11354431
Hey,
This is the most popular example for recursion
declare a function like this

getchr()
{
c=getch();
if(c != '*') getchr();
putchar(c);
}

What this function does is recursively call itself and read a character. If the character is not a termination character (* in our case), it again calls itself and accepts a character.

When the termination character is entered, it recursively spirals thru the last call to the first call of the function. This specific behaviour is obtained by the placement of the getchr() and putchar() functions.

Try this and give me your feedback

Manav
 
06.21.2004 at 01:47AM PDT, ID: 11357780
If your string is 20 characters long, there will be 20 variables c on the stack. Not temporary??
 
06.22.2004 at 12:00AM PDT, ID: 11366694
No. Because those 20 variables on the stack store the string just like a 20 character string which is not a temporary variable.

This is one of the standard questions you will find in amny books under the recursion chapter. This question is more about lateral thinking than technical.
The trick here is how to read the input. Rather than storing it in an array, store it in a fashion where u can inherently access it backwards. There are 2 methods, a link list or recursion. For link list, u again need a temp variable, but not for recursion.

So thats your answer. FOr 20 bytes string, u will be using 20 'c' variables in the STACK RATHER THAN THE HEAP. Thats the trick!!

Manav

Th
 
09.02.2004 at 10:42PM PDT, ID: 11970430
Paragm78,

This old question needs to be finalized — accept an answer, split points, or get a refund.  For information on your options, please see
  http://www.experts-exchange.com/help.jsp#hs5

EXPERTS:
Please help with this cleanup process! Post closing recommendations within 4 days.

jmcg
EE Cleanup Volunteer
 
09.09.2004 at 12:16PM PDT, ID: 12020286
Nothing has happened on this question in more than 3 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
PAQ, forfeit points.

Please leave any comments here within the next four days.

jmcg
EE Cleanup Volunteer
 
09.13.2004 at 06:08PM PDT, ID: 12050355
PAQed - no points refunded (of 20)

Computer101
E-E Admin
Accepted Solution
 
 
20080236-EE-VQP-29