【字符串處理算法】回文判斷的算法設(shè)計(jì)及C代碼實(shí)現(xiàn)
一、需求描述
輸入一個(gè)字符串,編寫程序判斷這個(gè)字符串是否是回文串。
為了便于說(shuō)明,設(shè)定輸入的字符串分為中文字符串和非中文字符串兩種。其中,中文字符串中僅包含中文字符,非中文字符串中不包含中文字符。
所謂回文串,是指正讀和反讀都一樣的字符串。下面舉幾個(gè)例子予以說(shuō)明:
1.“level”是一個(gè)非中文字符的回文串,因?yàn)檎x和反讀都是“level”。
2.“Good”不是一個(gè)非中文字符的回文串。
3.“我愛(ài)我”是一個(gè)中文字符的回文串,因?yàn)檎x和反讀都是“我愛(ài)我”。
4.“我愛(ài)你”不是一個(gè)中文字符的回文串。
二、算法設(shè)計(jì)
對(duì)于非中文字符的回文串的判斷比較簡(jiǎn)單,我們只要以字符串的中間為原點(diǎn),比較前后對(duì)應(yīng)的字符是否相等就可以了;但對(duì)于中文字符的回文串的判斷要復(fù)雜一點(diǎn),因?yàn)橐粋€(gè)中文字符占兩個(gè)字節(jié),我們不能采用非中文字符的回文串的判斷方法,而是應(yīng)該先單獨(dú)獲取每個(gè)中文字符,然后再比較一前一后兩個(gè)字符是否相等。
程序的總體流程如圖1所示。
圖1 程序的總體流程
三、特殊流程考慮
在編寫程序的過(guò)程中,我們要對(duì)輸入的字符串的長(zhǎng)度及格式多做考慮,如:
1.如果輸入的字符串中只有一個(gè)字符,那么程序直接返回,不執(zhí)行后續(xù)流程,因?yàn)榛匚拇兄辽儆袃蓚€(gè)及以上的字符。
2.如果輸入的中文串中含有非中文字符,或者是輸入的非中文串中含有中文字符,那么程序直接返回,不執(zhí)行后續(xù)流程。
四、程序代碼
- /**********************************************************************
- * 版權(quán)所有 (C)2016, Zhou Zhaoxiong。
- *
- * 文件名稱: PalindromicString.c
- * 文件標(biāo)識(shí): 無(wú)
- * 內(nèi)容摘要: 回文判斷
- * 其它說(shuō)明: 形如madam, php, 2992, 1234321這樣的串就是回文串
- * 當(dāng)前版本: V1.0
- * 作 者: Zhou Zhaoxiong
- * 完成日期: 20160222
- *
- **********************************************************************/
- #include <stdio.h>
- // 重新定義數(shù)據(jù)類型
- typedef signed char INT8;
- typedef int INT32;
- typedef unsigned int UINT32;
- // 全局變量聲明, 用于存放漢字, ***支持100個(gè)漢字
- INT8 gszStrCharArray[101][5] = {0};
- UINT32 giCharNum = 0;
- // 函數(shù)聲明
- void JudgePalindromicString(INT8 *pszInputStr, UINT32 iInputStrLen, UINT32 iStrType);
- void GetChineseChars(INT8 *pszInputStr);
- INT32 JudgeStrFormat(INT8 *pszInputStr, UINT32 iStrType);
- /**********************************************************************
- * 功能描述: 主函數(shù)
- * 輸入?yún)?shù): 無(wú)
- * 輸出參數(shù): 無(wú)
- * 返 回 值: 0-執(zhí)行成功 其它-執(zhí)行失敗
- * 其它說(shuō)明: 無(wú)
- * 修改日期 版本號(hào) 修改人 修改內(nèi)容
- * ---------------------------------------------------------------------
- * 20160222 V1.0 Zhou Zhaoxiong 創(chuàng)建
- ***********************************************************************/
- INT32 main()
- {
- UINT32 iStrType = 0;
- INT32 iRetVal = 0;
- INT8 szInputStr[100] = {0};
- printf("Please input the string type(1:中文字符串,2:非中文字符串): \n");
- scanf("%d", &iStrType);
- printf("Please input the string: \n");
- scanf("%s", szInputStr);
- // 判斷輸入的字符串是中文字符串或者是非中文字符串
- iRetVal = JudgeStrFormat(szInputStr, iStrType);
- if (iRetVal != 0)
- {
- return -1;
- }
- if (iStrType == 1) // 如果輸入的是中文串, 則先獲取各個(gè)中文字符
- {
- GetChineseChars(szInputStr);
- if (giCharNum <= 1) // 只輸入了一個(gè)字符, 直接返回
- {
- printf("%s has only one character, please check!\n", szInputStr);
- return -1;
- }
- }
- else if (iStrType == 2)
- {
- if (strlen(szInputStr) <= 1) // 只輸入了一個(gè)字符, 直接返回
- {
- printf("%s has only one character, please check!\n", szInputStr);
- return -1;
- }
- }
- // 判斷輸入的字符串是否為回文串
- JudgePalindromicString(szInputStr, strlen(szInputStr), iStrType);
- return 0;
- }
- /**********************************************************************
- * 功能描述:判斷輸入的字符串是否為回文串
- * 輸入?yún)?shù):pszInputStr-輸入的字符串
- iInputStrLen-輸入的字符串的長(zhǎng)度
- iStrType-輸入的字符串的類型
- * 輸出參數(shù):無(wú)
- * 返 回 值:無(wú)
- * 其它說(shuō)明:無(wú)
- * 修改日期 版本號(hào) 修改人 修改內(nèi)容
- * -------------------------------------------------------------------
- * 20160222 V1.0 Zhou Zhaoxiong 創(chuàng)建
- ***********************************************************************/
- void JudgePalindromicString(INT8 *pszInputStr, UINT32 iInputStrLen, UINT32 iStrType)
- {
- UINT32 iPosFlag = 0;
- if (NULL == pszInputStr)
- {
- return;
- }
- if (iStrType == 1) // 中文字符串
- {
- for (iPosFlag = 0; iPosFlag < giCharNum/2; iPosFlag ++)
- {
- if (strcmp(gszStrCharArray[iPosFlag], gszStrCharArray[giCharNum-1-iPosFlag]) != 0) // 有對(duì)應(yīng)字符不相等
- {
- printf("%s is not a palindromic string!\n", pszInputStr);
- return;
- }
- }
- }
- if (iStrType == 2) // 非中文字符串
- {
- // 從中間分開, 一前一后兩個(gè)字符互相比較, 如果全部對(duì)應(yīng)相等, 則是回文串
- for (iPosFlag = 0; iPosFlag < iInputStrLen/2; iPosFlag ++)
- {
- if (pszInputStr[iPosFlag] != pszInputStr[iInputStrLen-1-iPosFlag]) // 有對(duì)應(yīng)字符不相等
- {
- printf("%s is not a palindromic string!\n", pszInputStr);
- return;
- }
- }
- }
- printf("%s is a palindromic string!\n", pszInputStr);
- return;
- }
- /**********************************************************************
- * 功能描述:獲取輸入的字符串中的每個(gè)中文字符
- * 輸入?yún)?shù):pszInputStr-輸入的字符串
- iInputStrLen-輸入的字符串的長(zhǎng)度
- * 輸出參數(shù):無(wú)
- * 返 回 值:無(wú)
- * 其它說(shuō)明:無(wú)
- * 修改日期 版本號(hào) 修改人 修改內(nèi)容
- * -------------------------------------------------------------------
- * 20160222 V1.0 Zhou Zhaoxiong 創(chuàng)建
- ***********************************************************************/
- void GetChineseChars(INT8 *pszInputStr)
- {
- UINT32 iPosFlag = 0;
- if (NULL == pszInputStr)
- {
- return;
- }
- memset(gszStrCharArray, 0x00, sizeof(gszStrCharArray));
- giCharNum = 0;
- while (iPosFlag < strlen(pszInputStr))
- {
- snprintf(gszStrCharArray[giCharNum], sizeof(gszStrCharArray[giCharNum])-1, "%c%c", pszInputStr[iPosFlag], pszInputStr[iPosFlag+1]);
- iPosFlagiPosFlag = iPosFlag + 2; // 每個(gè)中文字符占兩個(gè)字節(jié)
- giCharNum ++;
- }
- }
- /**********************************************************************
- * 功能描述:判斷輸入的字符串的格式是否正確
- * 輸入?yún)?shù):pszInputStr-輸入的字符串
- iStrType-輸入的字符串的類型
- * 輸出參數(shù):無(wú)
- * 返 回 值:0-格式正確 其它-格式不正確
- * 其它說(shuō)明:無(wú)
- * 修改日期 版本號(hào) 修改人 修改內(nèi)容
- * -------------------------------------------------------------------
- * 20160222 V1.0 Zhou Zhaoxiong 創(chuàng)建
- ***********************************************************************/
- INT32 JudgeStrFormat(INT8 *pszInputStr, UINT32 iStrType)
- {
- UINT32 iPosFlag = 0;
- if (NULL == pszInputStr)
- {
- return -1;
- }
- if (iStrType == 1) // 先判斷中文字符串
- {
- for (iPosFlag = 0; iPosFlag < strlen(pszInputStr); iPosFlag ++)
- {
- if (pszInputStr[iPosFlag] >= 0) // 不小于0則表示含有非中文字符
- {
- printf("%s has non-Chinese character, please check!\n", pszInputStr);
- return -2;
- }
- }
- }
- else if (iStrType == 2) // 再判斷非中文字符串
- {
- for (iPosFlag = 0; iPosFlag < strlen(pszInputStr); iPosFlag ++)
- {
- if (pszInputStr[iPosFlag] < 0) // 小于0則表示含有中文字符
- {
- printf("%s has Chinese character, please check!\n", pszInputStr);
- return -3;
- }
- }
- }
- else
- {
- printf("Please input the right string type!\n");
- return -4;
- }
- return 0;
- }
五、程序測(cè)試
我們將編寫好的程序“PalindromicString.c”上傳到Linux機(jī)器,并使用“gcc -g -o PalindromicStringPalindromicString.c”命令對(duì)該程序進(jìn)行編譯,生成“PalindromicString”文件。下面對(duì)程序進(jìn)行詳細(xì)的測(cè)試。
1.輸入中文字符串為“人上人”時(shí),程序運(yùn)行情況如下:
- Please input the string type(1:中文字符串,2:非中文字符串):
- 1
- Please input the string:
- 人上人
- 人上人 is a palindromic string!
2.輸入中文字符串為“我是誰(shuí)”時(shí),程序運(yùn)行情況如下:
- Please input the string type(1:中文字符串,2:非中文字符串):
- 1
- Please input the string:
- 我是誰(shuí)
- 我是誰(shuí) is not a palindromic string!
3.輸入非中文字符串為“level”時(shí),程序運(yùn)行情況如下:
- Please input the string type(1:中文字符串,2:非中文字符串):
- 2
- Please input the string:
- level
- level is a palindromic string!
4.輸入非中文字符串為“good”時(shí),程序運(yùn)行情況如下:
- Please input the string type(1:中文字符串,2:非中文字符串):
- 2
- Please input the string:
- good
- good is not a palindromic string!
5.輸入字符串為“你好good”時(shí),程序運(yùn)行情況如下:
- Please input the string type(1:中文字符串,2:非中文字符串):
- 1
- Please input the string:
- 你好good
- 你好good has non-Chinese character, pleasecheck!
6.輸入字符串為“good好”時(shí),程序運(yùn)行情況如下:
- Please input the string type(1:中文字符串,2:非中文字符串):
- 2
- Please input the string:
- good好
- good好 has Chinese character, pleasecheck!
7.輸入字符串類型非1或2時(shí),程序運(yùn)行情況如下:
- Please input the string type(1:中文字符串,2:非中文字符串):
- 3
- Please input the string:
- goog
- Please input the right string type!
可見,對(duì)于上面考慮到的幾種特殊情況,程序均能做出正確的處理。
六、需求擴(kuò)展
基于本文中的需求和程序,我們可考慮對(duì)需求進(jìn)行以下擴(kuò)展:
1.不限制輸入的字符串中只能是中文串或者非中文串,可以是中文字符和非中文字符的混合串。
2.當(dāng)輸入的字符串中是非中文串時(shí),要求該字符串的字符個(gè)數(shù)為偶數(shù)。即要求形如“goog”這樣的字符串為回文串,而像“level” 這樣的字符串不為回文串。
【本文是51CTO專欄作者周兆熊的原創(chuàng)文章,作者微信公眾號(hào):周氏邏輯(logiczhou)】