title: How to get the last char from a string
body: <p>I want to get the weight and the object in a list string( in this example I want to get the integer 501 and the string &quot;kg bag of sugar&quot;. But I don't know-how may string after the integer. but I do know how many spaces before and after the integer (that is why I do +3 since there are 2 spaces before integer and 1 space at the end). I got a segmentation fault in my code.</p>
  <p>Here is an example of what I am trying to do.</p>
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;math.h&gt;
  #include &lt;string.h&gt;
  
  /* get the weight and the object */
  int main(void) {   
      char line[50] = &quot;  501 kg bag of sugar&quot;; //we don't know how many char after integer 
      char afterint[50];
      long int weight, weight2;
      int lenofint;
      sscanf(line, &quot;%ld&quot;, &amp;weight);
      weight2 = weight;
      while (weight2 != 0) {
          weight2 = weight2 / 10;
          lenofint++;
      }
      
      afterint[0] = line[lenofint + 3]; // +3 since there are 2 spaces before integer and 1 space at the end
      //printf(&quot;%c&quot;, afterint);
      for (int j = 1; j &lt; (strlen(line) - lenofint - 3); j++) {
          afterint[j] = afterint[j] + line[j + lenofint + 3];
      }
      printf(&quot;%s&quot;, afterint);
  }
  </code></pre>