User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Nov 2007
Posts: 3
Reputation: sathishkumar.e is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
sathishkumar.e sathishkumar.e is offline Offline
Newbie Poster

Solution command line string swap

  #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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2005
Posts: 3,834
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 23
Solved Threads: 436
Colleague
Salem's Avatar
Salem Salem is offline Offline
banned

Re: command line string swap

  #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  
Join Date: Oct 2006
Location: India
Posts: 514
Reputation: Jishnu will become famous soon enough Jishnu will become famous soon enough 
Rep Power: 5
Solved Threads: 25
Jishnu's Avatar
Jishnu Jishnu is offline Offline
Posting Pro

Re: command line string swap

  #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  
Join Date: Nov 2007
Posts: 3
Reputation: sathishkumar.e is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
sathishkumar.e sathishkumar.e is offline Offline
Newbie Poster

Re: command line string swap

  #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  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,539
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 972
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Most Valuable Poster

Re: command line string swap

  #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.
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";
        
    }

}
<<Freelance Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 13
Solved Threads: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: command line string swap

  #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):
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.
Reply With Quote  
Join Date: Dec 2005
Posts: 3,834
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 23
Solved Threads: 436
Colleague
Salem's Avatar
Salem Salem is offline Offline
banned

Re: command line string swap

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

Re: command line string swap

  #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  
Join Date: Sep 2004
Posts: 6,507
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 31
Solved Threads: 487
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: command line string swap

  #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?
I'm here to prove you wrong.
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,539
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 972
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Most Valuable Poster

Re: command line string swap

  #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.
<<Freelance Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 9:12 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC