一、需求描述

给定一个长字符串和一个短字符串,编写程序判断短字符串中的所有字符是否都在长字符串中。如果是,则长字符串包含短字符串;反之,不包含。


为了尽量包含大多数情况,字符串中可以包含大小写英文字母、数字和各种标点符号,并且区分大小写字母。


下面举几个例子予以说明:

1.如果长字符串是“ABCDE”,短字符串是“ADC”,那么短字符串中的所有字符都在长字符串中,即长字符串包含了短字符串。


2.如果长字符串是“ABCDE”,短字符串是“ADCF”,那么短字符串中不是所有字符都在长字符串中,即长字符串不包含了短字符串。


3.如果长字符串是“ABCDE”,短字符串是“AAB”,那么短字符串中的所有字符都在长字符串中,即长字符串包含了短字符串。

 

二、算法设计

我们都知道,就像人体是由一个个的细胞组成一样,一个字符串是由一个个的字符组成。如果组成某个字符串的所有字符都在另一个字符串中,那么这个字符串就被另一个字符串包含。


因此,我们可以考虑先将两个字符串中的所有字符都找出来,再判断较短的字符串中的所有字符是否都出现在了较长的字符串中。如果是,那么两个字符串是包含与被包含的关系;如果不是,那么两个字符串则“形同陌路”。


程序的总体流程如图1所示。

【字符串处理算法】字符串包含的算法设计及C代码实现_字符串

图1 程序的总体流程

 

三、特殊流程考虑

在编写程序的过程中,我们要对输入的两个字符串的长度及格式多做考虑,如:

1.如果输入失误,导致短字符串的长度大于了长字符串,那么程序直接返回,不进行后续处理。


2.不允许在输入字符串的中间出现空格,如果出现了,只能把空格前面的内容作为输入的字符串。


3.输入字符串可以包含字母(区分大小写)、数字、标点符号等字符。


4.为了方便程序处理,设定较长的字符串最长为500个字节,较短的字符串最长为100个字节。

 

四、程序代码

 

/**********************************************************************
* 版权所有 (C)2016, Zhou Zhaoxiong。
*
* 文件名称: StringContains.c
* 文件标识: 无
* 内容摘要: 测试一个字符串是否是另一个字符串的子串
* 其它说明: 例如, "ABC"是"ABCD"的子串
* 当前版本: V1.0
* 作 者: Zhou Zhaoxiong
* 完成日期: 20160216
*
**********************************************************************/
#include


#include



// 重新定义数据类型
typedef signed char INT8;
typedef unsigned short int UINT16;
typedef int INT32;
typedef unsigned int UINT32;

// 存放字符串中的字符和格式的结构体
typedef struct
{
INT8 szStrCharArray[101][2]; // 字符串中不相同的字符的存放数组,最大支持100个
INT32 iStrCharCount; // 字符串中不相同的字符的个数
} StrInfo_T;

StrInfo_T gtLongerStrInfo = {0};
StrInfo_T gtShorterStrInfo = {0};


// 函数声明
void GetStrChar(INT8 *pszInputStr, INT32 iProcessFlag);
INT32 JudgeIfContainsStr();


/**********************************************************************
* 功能描述: 主函数
* 输入参数: 无
* 输出参数: 无
* 返 回 值: 0-执行成功 其它-执行失败
* 其它说明: 无
* 修改日期 版本号 修改人 修改内容
* -----------------------------------------------------------------
* 20160216 V1.0 Zhou Zhaoxiong 创建
***********************************************************************/
INT32 main()
{
INT8 szLongerStr[500] = {0};
INT8 szShorterStr[100] = {0};

UINT32 iContainFlag = 1; // 包含标志, 1-包含, 0-不包含

printf("Please input the longer string: \n");
scanf("%s", szLongerStr);
printf("LongerStr=%s\n", szLongerStr);

printf("Please input the shorter string: \n");
scanf("%s", szShorterStr);
printf("ShorterStr=%s\n", szShorterStr);

// 如果ShorterStr的长度大于LongerStr, 则直接返回
if (strlen(szShorterStr) > strlen(szLongerStr))
{
printf("%s is longer than %s, please check!\n", szShorterStr, szLongerStr);
return -1;
}

// 获取较长的字符串中的不同的字符
GetStrChar(szLongerStr, 1);

// 获取较短的字符串中的不同的字符
GetStrChar(szShorterStr, 2);

iContainFlag = JudgeIfContainsStr();
if (iContainFlag == 0)
{
printf("%s doesn't contain %s!\n", szLongerStr, szShorterStr);
}
else
{
printf("%s contains %s!\n", szLongerStr, szShorterStr);
}

return 0;
}


/**********************************************************************
* 功能描述: 获取字符串中不相同的字符及其个数
* 输入参数: pszInputStr-输入字符串
iProcessFlag-处理标志(1:处理长字符串, 2:处理短字符串)
* 输出参数: 无
* 返 回 值: 无
* 其它说明: 无
* 修改日期 版本号 修改人 修改内容
* ---------------------------------------------------------------
* 20160216 V1.0 Zhou Zhaoxiong 创建
***********************************************************************/
void GetStrChar(INT8 *pszInputStr, INT32 iProcessFlag)
{
INT32 iCharCount = 0; // 字符个数
INT8 szInputStr[501] = {0};
INT8 szCharBuf[2] = {0}; // 存放单个字符的缓存
INT32 iRepeatFlag = 0;
UINT32 iStrPosFlag = 0;
UINT32 iLoopFlag = 0;
UINT32 iInputStrLen = 0;

if (pszInputStr == NULL)
{
return;
}

iInputStrLen = strlen(pszInputStr);
if (iInputStrLen >= 500) // 最大支持100个字母
{
return;
}

memcpy(szInputStr, pszInputStr, iInputStrLen);

iCharCount = 0;

for (iStrPosFlag = 0; iStrPosFlag < iInputStrLen; iStrPosFlag ++)
{
iRepeatFlag = 0;

// 判断正要获取的字符是否已经存在了
memset(szCharBuf, 0x00, sizeof(szCharBuf));
memcpy(szCharBuf, szInputStr+iStrPosFlag, 1);

// 若与之前已经加入的字符重复, 则忽略
for (iLoopFlag = 0; iLoopFlag < iCharCount; iLoopFlag ++)
{
if (iProcessFlag == 1) // 处理长字符串
{
if (0 == strncmp(gtLongerStrInfo.szStrCharArray[iLoopFlag], szCharBuf, 1))
{
iRepeatFlag = 1; // 有重复的, 直接忽略
break;
}
}
else // 处理短字符串
{
if (0 == strncmp(gtShorterStrInfo.szStrCharArray[iLoopFlag], szCharBuf, 1))
{
iRepeatFlag = 1; // 有重复的, 直接忽略
break;
}
}
}

if (1 == iRepeatFlag)
{
continue;
}

if (iProcessFlag == 1) // 处理长字符串
{
strncpy(gtLongerStrInfo.szStrCharArray[iCharCount], szCharBuf, 1);
}
else // 处理短字符串
{
strncpy(gtShorterStrInfo.szStrCharArray[iCharCount], szCharBuf, 1);
}

iCharCount = iCharCount + 1;
}

if (iProcessFlag == 1) // 处理长字符串
{
gtLongerStrInfo.iStrCharCount = iCharCount;
}
else // 处理短字符串
{
gtShorterStrInfo.iStrCharCount = iCharCount;
}

return;
}


/**********************************************************************
* 功能描述: 判断长字符串是否包含了短字符串
* 输入参数: 无
* 输出参数: 无
* 返 回 值: 1-包含了 0-没有包含
* 其它说明: 无
* 修改日期 版本号 修改人 修改内容
* ---------------------------------------------------------------
* 20160216 V1.0 Zhou Zhaoxiong 创建
***********************************************************************/
INT32 JudgeIfContainsStr()
{
UINT32 iLongerLoopFlag = 0;
UINT32 iShorterLoopFlag = 0;
UINT32 iCharIdenticalFlag = 0;

// 判断较短的字符串中的字符是否全部都在较长的字符串中的字符中
for (iShorterLoopFlag = 0; iShorterLoopFlag < gtShorterStrInfo.iStrCharCount; iShorterLoopFlag ++)
{
iCharIdenticalFlag = 0;
for (iLongerLoopFlag = 0; iLongerLoopFlag < gtLongerStrInfo.iStrCharCount; iLongerLoopFlag ++)
{
if (strcmp(gtShorterStrInfo.szStrCharArray[iShorterLoopFlag], gtLongerStrInfo.szStrCharArray[iLongerLoopFlag]) == 0)
{
iCharIdenticalFlag = 1; // 字符相同
break;
}
}

if (iCharIdenticalFlag == 0) // 表示两个字符串中有不相同的字符
{
return 0;
}
}

return 1;
}

 

五、程序测试

我们将编写好的程序“StringContains.c”上传到Linux机器,并使用“gcc -g -o StringContainsStringContains.c”命令对该程序进行编译,生成“StringContains”文件。下面对程序进行详细的测试。


1.输入较长字符串为“ABCDF”、较短字符串为“AF”时,程序运行情况如下:

Please input the longer string:

ABCDF

LongerStr=ABCDF

Please input the shorter string:

AF

ShorterStr=AF

ABCDF contains AF!

 

2.输入较长字符串为“AB”、较短字符串为“ABC”时,程序运行情况如下:

Please input the longer string:

AB

LongerStr=AB

Please input the shorter string:

ABC

ShorterStr=ABC

ABC is longer than AB, please check!

 

3.输入较长字符串为“awe”、较短字符串为“rf”时,程序运行情况如下:

Please input the longer string:

awe

LongerStr=awe

Please input the shorter string:

rf

ShorterStr=rf

awe doesn't contain rf!

 

4.输入较长字符串为“`11245”、较短字符串为“45”时,程序运行情况如下:

Please input the longer string:

`11245

LongerStr=`11245

Please input the shorter string:

45

ShorterStr=45

`11245 contains 45!

 

5.输入较长字符串为“123”、较短字符串为“123 45”时,程序运行情况如下:

Please input the longer string:

123

LongerStr=123

Please input the shorter string:

123 45

ShorterStr=123

123 contains 123!

 

可见,对于上面考虑到的几种特殊情况,程序均能做出正确的处理。

 

六、需求扩展

基于本文中的需求和程序,我们可考虑对需求进行以下扩展:

1.限制输入的字符串中只能包含字母,如果包含了其它字符,则直接退出而不进行处理。


2.如果较短的字符串中的所有字符虽然都在较长的字符串中,但某个字符在较短的字符串中出现的次数大于了在较长的字符串中出现的次数,那么就认为较长的字符串不包含较短的字符串。