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.
2 odpowiedzi
+ 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);
+ 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';