•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 455,970 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,773 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 2491 | Replies: 23
![]() |
Do you have an example of what you're trying to do?
Since you said "swap without a temp", perhaps you're referring to this dumb trick?
http://c-faq.com/expr/xorswapexpr.html
Since you said "swap without a temp", perhaps you're referring to this dumb trick?
http://c-faq.com/expr/xorswapexpr.html
Search for your problem before posting. It is likely that someone might have faced the same problem earlier.
"You know you're a computer geek when you try to shoo a fly away from the monitor screen with your cursor. That just happened to me. It was scary." - Juuso Heimonen.
"The only truly secure computer is one buried in concrete, with the power turned off and the network cable cut." - Anonymous.
"The only truly secure computer is one buried in concrete, with the power turned off and the network cable cut." - Anonymous.
•
•
Join Date: Nov 2007
Posts: 3
Reputation:
Rep Power: 0
Solved Threads: 0
•
•
•
•
Do you have an example of what you're trying to do?
Since you said "swap without a temp", perhaps you're referring to this dumb trick?
http://c-faq.com/expr/xorswapexpr.html
Hi,
Thank u for ur reply.
My aim is to swap two strings without using a temporary pointer or char array, but i have to use command line arguments for getting the two strings as input.
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,539
Reputation:
Rep Power: 40
Solved Threads: 972
I don't think there is a general way to do it. A char data type is not large enough to use the method that Salem posted. And my compiler VC++ 2005 Express doesn't allow this either even when I typcast the pointers to unsigned long.
int main(int argc, char* argv[])
{
if(argc == 3)
{
(unsigned long)argv[1] ^= (unsigned long)argv[2] ^= (unsigned long)argv[1] ^= (unsigned long)argv[2];
cout << argv[1] << "\n";
cout << argv[2] << "\n";
}
}•
•
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
Reputation:
Rep Power: 13
Solved Threads: 193
At the risk of being repetitive for having mentioned this before, you may not have write access to your command line arguments. Moreover, you can't change their size. I think it is perfectly within reason to copy them into local character arrays:
That example for the trick is horribly broken, but it works just fine when done right. Keep the sequence points intact, and only try to swap ordinal values of the same size (char s match both criteria):
Hope this helps.
C Syntax (Toggle Plain Text)
int main( int argc, char *argv[] ) { char *s1, *s2; int len, len2; if (argc < 3) { printf( "fooey!\n" ); return 1; } /* len <-- size of the longest string */ len = strlen( argv[ 1 ] ); len2 = strlen( argv[ 2 ] ); len = (len < len2) ? len2 : len; /* get the strings */ s1 = (char *)malloc( len ); s2 = (char *)malloc( len ); strcpy( s1, argv[ 1 ] ); strcpy( s2, argv[ 2 ] ); printf( "Given the strings:\n" ); printf( "1. %s\n", s1 ); printf( "2. %s\n", s2 ); /* (do your swap here) */ printf( "Now the strings are:\n" ); printf( "1. %s\n", s1 ); printf( "2. %s\n", s2 ); free( s2 ); free( s1 ); return 0; }
That example for the trick is horribly broken, but it works just fine when done right. Keep the sequence points intact, and only try to swap ordinal values of the same size (char s match both criteria):
char a = '1', b = '2'; printf( "%c%c\n", a, b ); a ^= b, b ^= a, a ^= b; printf( "%c%c\n", a, b );
Hope this helps.
Last edited by Duoas : Nov 27th, 2007 at 1:10 am.
•
•
Join Date: Nov 2007
Posts: 18
Reputation:
Rep Power: 1
Solved Threads: 0
Please don't do that again!
>you may not have write access to your command line arguments
Both C89 and C99 say otherwise, and they both use exactly the same wording:
>Moreover, you can't change their size.
This is correct though.
>My aim is to swap two strings without using a temporary pointer
>or char array, but i have to use command line arguments for
>getting the two strings as input.
If the question is to copy the actual arguments (eg. argv[1] and argv[2]), the only viable solution would have to assume that both of those strings are exactly the same length:
>Please don't do that again!
Not enough information. Why shouldn't he do it again?
Both C89 and C99 say otherwise, and they both use exactly the same wording:
•
•
•
•
The parameters argc and argv and the strings pointed to by the argv array shall be modifiable by the program, and retain their last-stored values between program startup and program termination.
This is correct though.
>My aim is to swap two strings without using a temporary pointer
>or char array, but i have to use command line arguments for
>getting the two strings as input.
If the question is to copy the actual arguments (eg. argv[1] and argv[2]), the only viable solution would have to assume that both of those strings are exactly the same length:
c Syntax (Toggle Plain Text)
#include <stdio.h> int main ( int argc, char *argv[] ) { if ( argc > 2 ) { int i = 0; printf ( "Before: '%s' '%s'\n", argv[1], argv[2] ); while ( argv[1][i] != '\0' ) { char save = argv[1][i]; argv[1][i] = argv[2][i]; argv[2][i++] = save; } printf ( "After: '%s' '%s'\n", argv[1], argv[2] ); } return 0; }
Not enough information. Why shouldn't he do it again?
I'm here to prove you wrong.
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,539
Reputation:
Rep Power: 40
Solved Threads: 972
Narue: Oh yes, when I said its not possible I was not thinking outside the box but assumed "but without using the temp. string,pointer or array" included a temp variable as in your line 11.
![]() |
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- path on the command-line (C++)
- Command-line argument syntax (C++)
- To fetch data through command line arguments (C++)
- Creating a Command Line Parser (C++)
- Help with Creating a Command Line Menu (C++)
- What is the Command-Line Argument for??? (C++)
- Simple Command Line interface - (Java)
- Windows Main and Command Line Parameters (C)
- Command line calculator (C)
- Command Line Parameter (C++)
Other Threads in the C Forum
- Previous Thread: Memory refrence error when printing linked list
- Next Thread: Description of the interpolation search algorithm



Linear Mode