How can I perfectly truncate a string in c?

℡╲_俬逩灬. 提交于 2019-12-02 09:33:50

You have this result because you forgot to add the null string terminator in the end of a[0].

There's several ways to do it. My favorite one is to leave line as is: since it seems you want the truncated string elsewhere, you souldn't modify the source. In that case, you can replace:

//Tries to truncate "line" to 20 characters
line[20] = '\0';

//copying line to each character at a time
int x;
for(x = 0; x < 20; x++)
{
    a[0][x] = line[x];
}

with

//copying line to each character at a time
int x;
for(x = 0; x < 20; x++)
{
    a[0][x] = line[x];
}
a[0][20] = 0;
mad_mask

i think you are missing the null byte.

You can get more info about this at : Null byte and arrays in C

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!