| | |
command line string swap
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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:
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
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.
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.
C Syntax (Toggle Plain Text)
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"; } }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
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):
C Syntax (Toggle Plain Text)
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:
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?
New members chased away this month: 4
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
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: balanced k-way sort-merge
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays asterisks binarysearch calculate centimeter char convert copyanyfile copyimagefile copypdffile cprogramme createcopyoffile csyntax directory drawing dynamic executable fflush file fork frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop initialization interest km lazy linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling scripting segmentationfault send shape socketprograming spoonfeeding stack standard string strings structures student suggestions systemcall test testautomation unix user variable voidmain() wab win32api windows.h






