command line string swap

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2007
Posts: 3
Reputation: sathishkumar.e is an unknown quantity at this point 
Solved Threads: 0
sathishkumar.e sathishkumar.e is offline Offline
Newbie Poster

command line string swap

 
0
  #1
Nov 23rd, 2007
Hi,

I'm a newbie to this group. I heard a question in C to swap the two strings using command line arguments but without using the temp. string,pointer or array


Can anyone help me how to do this.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: command line string swap

 
0
  #2
Nov 23rd, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 514
Reputation: Jishnu will become famous soon enough Jishnu will become famous soon enough 
Solved Threads: 26
Jishnu's Avatar
Jishnu Jishnu is offline Offline
Posting Pro

Re: command line string swap

 
0
  #3
Nov 23rd, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3
Reputation: sathishkumar.e is an unknown quantity at this point 
Solved Threads: 0
sathishkumar.e sathishkumar.e is offline Offline
Newbie Poster

Re: command line string swap

 
0
  #4
Nov 27th, 2007
Originally Posted by Salem View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,625
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1495
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: command line string swap

 
0
  #5
Nov 27th, 2007
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.
  1. int main(int argc, char* argv[])
  2. {
  3. if(argc == 3)
  4. {
  5. (unsigned long)argv[1] ^= (unsigned long)argv[2] ^= (unsigned long)argv[1] ^= (unsigned long)argv[2];
  6. cout << argv[1] << "\n";
  7. cout << argv[2] << "\n";
  8.  
  9. }
  10.  
  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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: command line string swap

 
0
  #6
Nov 27th, 2007
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:
  1. int main( int argc, char *argv[] ) {
  2. char *s1, *s2;
  3. int len, len2;
  4.  
  5. if (argc < 3) {
  6. printf( "fooey!\n" );
  7. return 1;
  8. }
  9.  
  10. /* len <-- size of the longest string */
  11. len = strlen( argv[ 1 ] );
  12. len2 = strlen( argv[ 2 ] );
  13. len = (len < len2) ? len2 : len;
  14.  
  15. /* get the strings */
  16. s1 = (char *)malloc( len );
  17. s2 = (char *)malloc( len );
  18.  
  19. strcpy( s1, argv[ 1 ] );
  20. strcpy( s2, argv[ 2 ] );
  21.  
  22. printf( "Given the strings:\n" );
  23. printf( "1. %s\n", s1 );
  24. printf( "2. %s\n", s2 );
  25.  
  26. /* (do your swap here) */
  27.  
  28. printf( "Now the strings are:\n" );
  29. printf( "1. %s\n", s1 );
  30. printf( "2. %s\n", s2 );
  31.  
  32. free( s2 );
  33. free( s1 );
  34. return 0;
  35. }

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):
  1. char a = '1', b = '2';
  2. printf( "%c%c\n", a, b );
  3. a ^= b, b ^= a, a ^= b;
  4. printf( "%c%c\n", a, b );

Hope this helps.
Last edited by Duoas; Nov 27th, 2007 at 1:10 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: command line string swap

 
0
  #7
Nov 27th, 2007
printf( "%s %s\n", argv[2], argv[1] ); swaps without any trickery at all.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 18
Reputation: doublex is an unknown quantity at this point 
Solved Threads: 0
doublex doublex is offline Offline
Newbie Poster

Re: command line string swap

 
0
  #8
Nov 27th, 2007
Originally Posted by Duoas View Post
  1.  
  2. strcpy( s1, argv[ 1 ] );
  3. strcpy( s2, argv[ 2 ] );
Please don't do that again!
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,847
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 753
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: command line string swap

 
1
  #9
Nov 27th, 2007
>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:
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.
>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:
  1. #include <stdio.h>
  2.  
  3. int main ( int argc, char *argv[] )
  4. {
  5. if ( argc > 2 ) {
  6. int i = 0;
  7.  
  8. printf ( "Before: '%s' '%s'\n", argv[1], argv[2] );
  9.  
  10. while ( argv[1][i] != '\0' ) {
  11. char save = argv[1][i];
  12. argv[1][i] = argv[2][i];
  13. argv[2][i++] = save;
  14. }
  15.  
  16. printf ( "After: '%s' '%s'\n", argv[1], argv[2] );
  17. }
  18.  
  19. return 0;
  20. }
>Please don't do that again!
Not enough information. Why shouldn't he do it again?
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,625
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1495
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: command line string swap

 
0
  #10
Nov 27th, 2007
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC