64,637
社区成员
发帖
与我相关
我的任务
分享
/**
* @file mystrcat.c
* @brief
*/
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int isnum(const char *s)
{
int i, n;
n = strlen(s);
for (i = 0; i < n; i++)
if (!isdigit(s[i]))
return 0;
return 1;
}
char * mystrcat(char *s1, const char *s2)
{
if (isnum(s1) && isnum(s2))
sprintf(s1, "%d", strtol(s1, NULL, 0) + strtol(s2, NULL, 0));
else
strcat(s1, s2);
return s1;
}
int main(int argc, char *argv[])
{
char s1[20] = "Hello";
char s2[] = "World";
char s3[20] = "123";
char s4[] = "456";
printf("%s\n", mystrcat(s1, s2));
printf("%s\n", mystrcat(s3, s4));
return 0;
}
bool is_number(std::string const &str)
{
auto const it = std::find_if(std::begin(str), std::end(str), [](char ch){ return ch < 48 || ch > 57;});
return it == std::end(str);
}
std::string const mystrcat(std::string const &str1, std::string const &str2)
{
if(is_number(str1) && is_number(str2)){
double const first = std::stod(str1);
double const second = std::stod(str2);
return std::to_string(first + second);
}else{
return str1 + str2;
}
}
c++98
有情好心人士补上