title: Segmentation fault when using strlen
body: <p>I've been working on this for past two days but can't seem to figure out the problem. </p>
  
  <p>I'm reading from a file and saving it into the char array as a string but when I try using <code>strlen()</code> on the string, it crashes with a segmentation fault.</p>
  
  <p>Here is what I'm doing. </p>
  
  <pre><code>char read_string[25];
  fscanf(file, "%s", &amp;read_string);
  int length = strlen(read_string);
  </code></pre>
  
  <p>Another problem is when I copy contents of <code>read_string</code> into another char array, it adds random characters at the end.</p>
  
  <p>Here is what I am doing</p>
  
  <pre><code>char input[25];
  for(i=0; i&lt;25; i++)
  {
      if (read_string[i] == '.')
          break;
      else
          input[i] = read_string[i];
  }
  </code></pre>
  
  <p>I need to copy everything before the period is read string into input array.</p>
  
  <p>P.S. I am using <code>fscanf()</code> because the file is well structured. </p>
  
  <p>Here is my code if needed.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  
  main()
  {
  //variables
  FILE *rFile; 
  int num_states;                 //number of states
  int num_alphabet;               //number of alphabets
  int num_acstates;               //number of accepting states
  int ac_state;                   //the accepting state
  char alphabet[num_alphabet];    //alphabet
  char read_string[25];   //assuming the input will be maximum of 25 character
  char input[25];
  int i,j,x;                      //indexes for the loop
  
  
  //open the file to read
  rFile = fopen("dfa11", "r");
  
  if (rFile != NULL)
  {
      //do the program here
  
      //read number of states and number of alphabets
      fscanf(rFile, "%d %d", &amp;num_states, &amp;num_alphabet);
  
      //read the alphabet
      fscanf(rFile, "%s", &amp;alphabet); 
  
      //read the state transition table (matrix)
      int value = num_states*num_alphabet;
      char table[num_states][num_alphabet];
  
      for(i=0; i&lt;num_states; i++)
      {
          for(j=0; j&lt;num_alphabet; j++)
          {
              fscanf(rFile, "%d", &amp;table[i][j]);
          }
      }
  
      //read number of accepting states and accepting state
      fscanf(rFile, "%d", &amp;num_acstates);
      fscanf(rFile, "%d", &amp;ac_state);
  
      //read the input string from the file
      fscanf(rFile, "%s", &amp;read_string);
  
      for(i=0; i&lt;25; i++)
      {
          if (read_string[i] == '.')
              break;
          else
              input[i] = read_string[i];
      }
  
      //strncpy(input, read_string, j);
  
      printf("%d\n", strlen(read_string));
      printf("%d\n", strlen(input));
      printf("%s\n", read_string);
      printf("%s\n", input);
  
  
      //close the file
      fclose(rFile);
  }
  
  else
  {
      printf("File could not be found");
  }
  }
  </code></pre>