title: How to swap string in C?
body: <p>I am trying to make a function in C which will swap two string variables but something went wrong and the program crashes.</p>
  
  <p>Please have a look at my code and tell me where I made a mistake:</p>
  
  <pre><code>#include &lt;string.h&gt;
  
  void strswap(char name1[], char name2[])    // to swap two strings
  {
      int lengthname1, lengthname2;
      lengthname1 = strlen(name1);
      lengthname2 = strlen(name2);
      char temporaryname1[100];
      char temporaryname2[100];
      int x;
      int y;
      // till just the declaration
      for (int x = 0; x &lt; lengthname1; lengthname1++) {
          temporaryname1[x] = name1[x];
          name1[x] = ' ';
      }
      // copying the value of name1 in temporaryname1
      for (int y = 0; y &lt; lengthname2; lengthname2++) {
          temporaryname2[x] = name2[x];
          name2[x] = ' ';
      }
      // copying the value of name2 in temporaryname2
      for (int x = 0; x &lt; lengthname1; lengthname1++) {
          name1[x] = temporaryname2[x];
      }
      for (int y = 0; y &lt; lengthname2; lengthname2++) {
          name2[x] = temporaryname1[x];
      }
  }
  
  
  #include &lt;stdio.h&gt;
  int main()
  {
      char name[] = "hello";
      char name2[] = "hi";
      printf("before swapping: %s %s\n", name, name2);
      strswap(name, name2);
      printf("after swapping: %s %s\n", name, name2);
  }
  </code></pre>
  
  <p>EDIT:- I have corrected the program and it is working properly. Soon my header file is going to work with some other modules. Thank you all for your help and especially to @Micheal</p>