CSDN论坛上网友的提问,如下:
C语言版本:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define LEN 1001
int str_case_cmp(char *src, char *dst)
{
for(; toupper(*src) == toupper(*dst); src++, dst++)
{
if(*src == '\0') return 0;
}
return *src - *dst;
}
int main(void)
{
int result = 0;
char src[LEN], dst[LEN];
fgets(src, LEN, stdin);
fgets(dst, LEN, stdin);
if(strlen(src) <= strlen(dst))
{
if(str_case_cmp(src, dst - strlen(src) + strlen(dst)) == 0) result = 1;
}
else
{
if(str_case_cmp(src + strlen(src) - strlen(dst), dst) == 0) result = 1;
}
printf("%s\n", result == 1 ? "True" : "False");
return 0;
}
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define LEN 1001
int main(void)
{
int result = 0;
char src[LEN], dst[LEN];
fgets(src, LEN, stdin);
fgets(dst, LEN, stdin);
char *s = src + strlen(src);
char *d = dst + strlen(dst);
for(; toupper(*s) == toupper(*d) && s >= src && d >= dst; s--, d--);
if(s < src || d < dst) result = 1;
printf("%s\n", result == 1 ? "True" : "False");
return 0;
}
C++语言版本:
#include <cctype>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
int main(void)
{
int result = 0;
string src, dst;
getline(cin, src);
getline(cin, dst);
transform(src.begin(), src.end(), src.begin(), toupper);
transform(dst.begin(), dst.end(), dst.begin(), toupper);
reverse(src.begin(), src.end());
reverse(dst.begin(), dst.end());
if (src.size() >= dst.size())
{
if (src.compare(0, dst.size(), dst) == 0) result = 1;
}
else
{
if (dst.compare(0, src.size(), src) == 0) result = 1;
}
cout << (result == 1 ? "True" : "False") << endl;
return 0;
}
#include <cctype>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
int main(void)
{
int result = 0;
string src, dst;
getline(cin, src);
getline(cin, dst);
transform(src.begin(), src.end(), src.begin(), [](int c) {return toupper(c); });
transform(dst.begin(), dst.end(), dst.begin(), [](int c) {return toupper(c); });
reverse(src.begin(), src.end());
reverse(dst.begin(), dst.end());
if (src.size() >= dst.size())
{
if (src.compare(0, dst.size(), dst) == 0) result = 1;
}
else
{
if (dst.compare(0, src.size(), src) == 0) result = 1;
}
cout << (result == 1 ? "True" : "False") << endl;
return 0;
}