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.
Author And Source
이 문제에 관하여(Algorithm 5 - Remove First and Last Character), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://velog.io/@ad-astra/Algorithm-5-Remove-First-and-Last-Character
저자 귀속: 원작자 정보가 원작자 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.
Author And Source
이 문제에 관하여(Algorithm 5 - Remove First and Last Character), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@ad-astra/Algorithm-5-Remove-First-and-Last-Character저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)