Nothing there is saying that the substring is invalid: you get what I would expect.
If I run your code at onlinegdb.com, I get a different result (if I ignore the copious compiler warnings):
string "G06111412/031/PL05"- has lenght 18 char, cut -13-char substring is "G06111412/031V"- lenght 14
And that's because it treats memory slightly differently to your compiler: by default it zeros memory when assigned - yours doesn't.
In C, a string is terminated by a
null
character - so when you extract 13 characters from your 18 character input
strncpy
copies the data up to the character limit (13) or the first null character
whichever comes first. In your case, that means that 13 characters are copied into your 14 character buffer and the last character position is never touched - it remains unassigned just as it was when your compiler allocated the memory. If by luck it is a
null
(which has odds of 256:1) then you will get a C string what works fine. If it doesn't, then the string continues utill; a null is located - and you get a length of 15, 16, 1024, whatever and random garbage printed after teh actual string data.
Add this line, and your problem will go away:
char c_substring[lengcut + 1];
strncpy(c_substring, str, lengcut);
c_substring[lengcut] = '\0';
But ... do yourself a favour and sort out the compiler warnings and notes before you run your app! They could be hiding errors and that means the compiler doesn't produce an EXE file so when you run your app it's the last version that did compiler you execute and that gets very confusing to test ... :D