Click here to Skip to main content
15,889,536 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to swap two strings using call by value as well as call by reference. Also, I want to ask the user to input the strings. I want to understand the concept more deeply so please help. 


What I have tried:

#include<stdio.h> 
void swap(char *str1, char *str2) 
{ 
  char *temp = str1; 
  str1 = str2; 
  str2 = temp; 
}   
   
int main() 
{ 
  char *str1 = "geeks"; 
  char *str2 = "forgeeks"; 
  swap(str1, str2); 
  printf("str1 is %s, str2 is %s", str1, str2); 
  getchar(); 
  return 0; 
} 


but, I want the users to input the strings
Posted
Updated 18-Jul-20 11:12am
Comments
KarstenK 19-Jul-20 3:46am    
You only swap the pointers to the string - not the string memory content.

Quote:
How can I swap two strings using call by value

Short answer: you don't.
By doing a call by value, you remove any link with the original variable. This is the reason of having 2 different ways of calling.
When calling by reference, you give a link to original valiable, when calling by value, there is no link to original variable.
 
Share this answer
 
While Patrice T is right, it is possible - but to do it you start complicating your code.
Because calling by value (the default for C) always passes a copy of a parameter any changes you make to that variable inside the function aren't reflected in the outside world - which is a very good thing! Image if you passed everything by reference:
C++
void Double(int x)
  {
  x = x * 2;
  }
Then call it:
C++
int val = 666;
Double(val);
printf("%u", val);
Will print what you expect: 1332
But that's not the only way you could call it:
C++
Double(666);
printf("%u", 666);
What should that print? 1332? 666? If it's the first, then your code is going to get really confusing, really fast! And if it's second ... what happened to the function call?

So parameters are passed by value, not by reference, so the only way to influence the outside world is by passing a copy of the address of the value and then the function can change its value.
C++
void Double2(int* x)
  {
  *x = *x * 2;
  }

C++
int val = 666;
Double2(&val);
printf("%u", val);
Will print what you expect: 1332
And
C++
Double2(&666);
printf("%u", 666);
Will not compile as you can't take the address of a constant value.

Which leads us back to your problem: with pass by value, how do you swap two pointers?
Simple: you pass a pointer to the pointer to the char:
C++
void swap(char **str1, char **str2) 
  { 
  char *temp = *str1; 
  *str1 = *str2; 
  *str2 = temp; 
  }
And then you pass the address of the pointer:
C++
char *str1 = "geeks"; 
char *str2 = "forgeeks"; 
swap(&str1, &str2); 
printf("str1 is %s, str2 is %s", str1, str2);


Make sense?
 
Share this answer
 
Take a lot at gets - C++ Reference[^] to accept user input.

The only way I know of to swap strings by value involves making a temporary copy and then copying back to the original buffers. That is a way fraught with peril because it depends on buffer sizes being adequate to hold the new strings. In fact, it is so dangerous I think it best not to even show it to you. It could be made safer by passing buffer sizes but that adds a another layer of complexity.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900