title: Replacing a substring in a string with another substring in c using array of pointers to string
body: <p>I am trying to replace a substring inside a string with another substring. However, I am getting a segmentation fault at line strcpy(p, str2); I do not understand why.
  Here is my code</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;string.h&gt;
  #include&lt;stdlib.h&gt;
  
  
  int main()
  {
      char *str[] = {"We will teach you how to",
          "Move a mountain",
          "Level a building",
          "Erase the past",
          "Make a million",
          "...all through C"
      };
      char str1[20], str2[20];
      printf("Enter first string\n");
      scanf("%s", str1);
      printf("Enter second string\n");
      scanf("%s", str2);
      char *news, *t, *p;
      int i;
      if(strlen(str2) &gt; strlen(str1))
      {
          printf("Second string should be smaller than first string\n");
          exit(1);
      }
      for(i = 0; i &lt; 6; i++)
      {
          p = strstr(str[i], str1);
          if(p)
          {
              news = p + strlen(str1);
              strcpy(t,news);
              strcpy(p, s);
              strcat(p,t);
              break;
          }
      }
      printf("the new string is\n");
      for( i = 0; i &lt; 6; i++)
      {
          printf("%s\n", str[i]);
      }
      return 0;
  }
  </code></pre>