需求:将常量字符"2021-04-30"中的字符“-”去掉,函数返回int类型的20210430的日期。
实现:
#define CHAR_LEN 10
int del(const char* str, char c, int len)
{
int j = 0;
int i = 0;
int ret = 0;
char *temp = NULL;
temp = (char *)malloc(len + 1);
memcpy(temp, str, len + 1);
printf("temp is:%s\n", temp);
for (; temp[i] != '\0'; i++)
if(temp[i] != c)
temp[j++] = temp[i];
temp[j]='\0';
ret = atoi(temp);
free(temp);
return ret;
}
int main() {
char s[CHAR_LEN + 1] = "2021-04-30";
int date = del(s, '-', CHAR_LEN);
printf("result is: <%d>", date);
运行结果:
temp is:2021-04-30
result is: <20210430>