Algorithm 5 - Remove First and Last Character
Q.
Description:
It's pretty straightforward. Your goal is to create a function that removes the first and last characters of a string. You're given one parameter, the original string. You don't have to worry with strings with less than two characters.
A) char* remove_char(char* dst, const char* src)
{
char *ptr = dst;
while (*src)
{
if (*(src + 2) == '\0')
break;
*ptr = *(src + 1);
ptr++;
src++;
}
*ptr = '\0';
return dst;
}
another solution
char *remove_char(char* dst, const char* src) {
src ++;
strncpy(dst, src, strlen(src)-1);
dst[strlen(src)-1] = '\0';
return dst;
} -> 'strnpy' does not guarantee the null character.
Reference
この問題について(Algorithm 5 - Remove First and Last Character), 我々は、より多くの情報をここで見つけました
https://velog.io/@ad-astra/Algorithm-5-Remove-First-and-Last-Character
テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol
char* remove_char(char* dst, const char* src)
{
char *ptr = dst;
while (*src)
{
if (*(src + 2) == '\0')
break;
*ptr = *(src + 1);
ptr++;
src++;
}
*ptr = '\0';
return dst;
}
another solution
char *remove_char(char* dst, const char* src) {
src ++;
strncpy(dst, src, strlen(src)-1);
dst[strlen(src)-1] = '\0';
return dst;
} -> 'strnpy' does not guarantee the null character.
Reference
この問題について(Algorithm 5 - Remove First and Last Character), 我々は、より多くの情報をここで見つけました https://velog.io/@ad-astra/Algorithm-5-Remove-First-and-Last-Characterテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol