给定一个由若干英文单词组成的字符串,用递归生成一个由各首字母组成的缩写词(acronym),其中的the,a,an,of,for及and被忽略。
例如:输入The Asia-Pacific Economic Cooperation
输出 APEC
急!!!c++ 递归生成字符串首字母缩写
答案:3 悬赏:0
解决时间 2021-04-08 14:11
- 提问者网友:心裂忍耐
- 2021-04-07 15:52
最佳答案
- 二级知识专家网友:爱情是怎么炼成的
- 2021-04-07 16:42
#include
#include
#include
char *getAcronym(char *src, char *dest)
{
char arr[4];
char *p = src, *temp;
char *q = dest;
if (!*p)
return NULL;
if (isupper(*p))
{
temp = p;
switch (*p)
{
case 'A' : // 排除 a an and
if (*++temp == ' '
|| (*temp == 'n' && *++temp == ' ')
|| (*temp == 'd' && *++temp == ' '))
{
getAcronym(++p, q);
}
else
{
sprintf(q, "%c", *p);
getAcronym(++p, ++q);
}
break;
case 'O' : // 排除 of
if (*++temp == 'f' && *++temp == ' ')
{
getAcronym(++p, q);
}
else
{
sprintf(q, "%c", *p);
getAcronym(++p, ++q);
}
break;
case 'F' : // 排除 for
if (*++temp == 'o' && *++temp == 'r' && *++temp == ' ')
{
getAcronym(++p, q);
}
else
{
sprintf(q, "%c", *p);
getAcronym(++p, ++q);
}
break;
case 'T' : // 排除 the
if (*++temp == 'h' && *++temp == 'e' && *++temp == ' ')
{
getAcronym(++p, q);
}
else
{
sprintf(q, "%c", *p);
getAcronym(++p, ++q);
}
break;
default :
sprintf(q, "%c", *p);
getAcronym(++p, ++q);
break;
}
}
else
{
getAcronym(++p, q);
}
return NULL;
}
int main()
{
char s[] = {"The Asia-Pacific A Hw Of Lkd An Psad For Pds And Economic Cooperation"};
char d[20];
memset(d, 0, 10);
getAcronym(s, d);
printf("%s\n", d);
return 0;
}
#include
#include
char *getAcronym(char *src, char *dest)
{
char arr[4];
char *p = src, *temp;
char *q = dest;
if (!*p)
return NULL;
if (isupper(*p))
{
temp = p;
switch (*p)
{
case 'A' : // 排除 a an and
if (*++temp == ' '
|| (*temp == 'n' && *++temp == ' ')
|| (*temp == 'd' && *++temp == ' '))
{
getAcronym(++p, q);
}
else
{
sprintf(q, "%c", *p);
getAcronym(++p, ++q);
}
break;
case 'O' : // 排除 of
if (*++temp == 'f' && *++temp == ' ')
{
getAcronym(++p, q);
}
else
{
sprintf(q, "%c", *p);
getAcronym(++p, ++q);
}
break;
case 'F' : // 排除 for
if (*++temp == 'o' && *++temp == 'r' && *++temp == ' ')
{
getAcronym(++p, q);
}
else
{
sprintf(q, "%c", *p);
getAcronym(++p, ++q);
}
break;
case 'T' : // 排除 the
if (*++temp == 'h' && *++temp == 'e' && *++temp == ' ')
{
getAcronym(++p, q);
}
else
{
sprintf(q, "%c", *p);
getAcronym(++p, ++q);
}
break;
default :
sprintf(q, "%c", *p);
getAcronym(++p, ++q);
break;
}
}
else
{
getAcronym(++p, q);
}
return NULL;
}
int main()
{
char s[] = {"The Asia-Pacific A Hw Of Lkd An Psad For Pds And Economic Cooperation"};
char d[20];
memset(d, 0, 10);
getAcronym(s, d);
printf("%s\n", d);
return 0;
}
全部回答
- 1楼网友:哭不代表软弱
- 2021-04-07 18:42
int length( char *lpstring )
{
if ( !lpstring || !(*lpstring) )
return 0;
else
return length( ++lpstring ) + 1;
}
- 2楼网友:迷人小乖乖
- 2021-04-07 17:11
根据例子,默认为首字母都是大写的哦
写的很乱,见谅了,嘿嘿~~~~~~~~~~~~
#include
#include
#include
char *getAcronym(char *src, char *dest)
{
char arr[4];
char *p = src, *temp;
char *q = dest;
if (!*p)
return NULL;
if (isupper(*p))
{
temp = p;
switch (*p)
{
case 'A' : // 排除 a an and
if (*++temp == ' '
|| (*temp == 'n' && *++temp == ' ')
|| (*temp == 'd' && *++temp == ' '))
{
getAcronym(++p, q);
}
else
{
sprintf(q, "%c", *p);
getAcronym(++p, ++q);
}
break;
case 'O' : // 排除 of
if (*++temp == 'f' && *++temp == ' ')
{
getAcronym(++p, q);
}
else
{
sprintf(q, "%c", *p);
getAcronym(++p, ++q);
}
break;
case 'F' : // 排除 for
if (*++temp == 'o' && *++temp == 'r' && *++temp == ' ')
{
getAcronym(++p, q);
}
else
{
sprintf(q, "%c", *p);
getAcronym(++p, ++q);
}
break;
case 'T' : // 排除 the
if (*++temp == 'h' && *++temp == 'e' && *++temp == ' ')
{
getAcronym(++p, q);
}
else
{
sprintf(q, "%c", *p);
getAcronym(++p, ++q);
}
break;
default :
sprintf(q, "%c", *p);
getAcronym(++p, ++q);
break;
}
}
else
{
getAcronym(++p, q);
}
return NULL;
}
int main()
{
char s[] = {"The Asia-Pacific A Hw Of Lkd An Psad For Pds And Economic Cooperation"};
char d[20];
memset(d, 0, 10);
getAcronym(s, d);
printf("%s\n", d);
return 0;
}
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯