0

eliminating some string in string

https://code.sololearn.com/cL6v2zNOcaTa/#c I want to eliminate some string in string. get number and eliminate some string starting from end. But I have some problem in this code.

7th Jun 2019, 2:08 PM
김도현
김도현 - avatar
2 Respuestas
+ 3
str[len-num] Is a single character but the function strcpy expects a string. To get a string starting from the position len-num, you should add the index to the pointer directly, like this: strcpy(str+len-num, str+len);
7th Jun 2019, 2:39 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 1
In addition to Kinshuk Vasisht's reply: If you want to eliminate from the end you can just set the null character at that position like: str[len - num] = '\0'; If you want to copy it to a new array you should probably use strncpy as it is more secure. https://en.cppreference.com/w/c/string/byte/strncpy char destination[128]; strncpy( destination, str, len - num ); destination[len - num] = '\0';
7th Jun 2019, 2:44 PM
Dennis
Dennis - avatar