自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

【字符串處理算法】回文判斷的算法設(shè)計(jì)及C代碼實(shí)現(xiàn)

開發(fā) 開發(fā)工具 算法
今天主要講講回文判斷的算法設(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ù)流程。

四、程序代碼

 
  1. /********************************************************************** 
  2. * 版權(quán)所有 (C)2016, Zhou Zhaoxiong。 
  3. * 文件名稱: PalindromicString.c 
  4. * 文件標(biāo)識(shí): 無(wú) 
  5. * 內(nèi)容摘要: 回文判斷 
  6. * 其它說(shuō)明: 形如madam, php, 2992, 1234321這樣的串就是回文串 
  7. * 當(dāng)前版本: V1.0 
  8. * 作    者: Zhou Zhaoxiong 
  9. * 完成日期: 20160222 
  10. **********************************************************************/ 
  11. #include <stdio.h> 
  12.  
  13.  
  14. // 重新定義數(shù)據(jù)類型 
  15. typedef signed   char  INT8; 
  16. typedef          int   INT32; 
  17. typedef unsigned int   UINT32; 
  18.  
  19. // 全局變量聲明, 用于存放漢字, ***支持100個(gè)漢字 
  20. INT8   gszStrCharArray[101][5] = {0};   
  21. UINT32 giCharNum               = 0
  22.  
  23.  
  24. // 函數(shù)聲明 
  25. void JudgePalindromicString(INT8 *pszInputStr, UINT32 iInputStrLen, UINT32 iStrType); 
  26. void GetChineseChars(INT8 *pszInputStr); 
  27. INT32 JudgeStrFormat(INT8 *pszInputStr, UINT32 iStrType); 
  28.  
  29.  
  30. /********************************************************************** 
  31. * 功能描述: 主函數(shù) 
  32. * 輸入?yún)?shù): 無(wú) 
  33. * 輸出參數(shù): 無(wú) 
  34. * 返 回 值: 0-執(zhí)行成功   其它-執(zhí)行失敗 
  35. * 其它說(shuō)明: 無(wú) 
  36. * 修改日期        版本號(hào)     修改人            修改內(nèi)容 
  37. * --------------------------------------------------------------------- 
  38. * 20160222        V1.0     Zhou Zhaoxiong      創(chuàng)建 
  39. ***********************************************************************/ 
  40. INT32 main() 
  41.     UINT32 iStrType        = 0
  42.     INT32  iRetVal         = 0
  43.     INT8   szInputStr[100] = {0}; 
  44.  
  45.     printf("Please input the string type(1:中文字符串,2:非中文字符串): \n"); 
  46.     scanf("%d", &iStrType); 
  47.      
  48.     printf("Please input the string: \n"); 
  49.     scanf("%s", szInputStr); 
  50.  
  51.     // 判斷輸入的字符串是中文字符串或者是非中文字符串 
  52.     iRetVal = JudgeStrFormat(szInputStr, iStrType); 
  53.     if (iRetVal != 0) 
  54.     { 
  55.         return -1; 
  56.     } 
  57.  
  58.     if (iStrType == 1)     // 如果輸入的是中文串, 則先獲取各個(gè)中文字符 
  59.     { 
  60.         GetChineseChars(szInputStr); 
  61.  
  62.         if (giCharNum <= 1)    // 只輸入了一個(gè)字符, 直接返回 
  63.         { 
  64.             printf("%s has only one character, please check!\n", szInputStr); 
  65.             return -1;  
  66.         } 
  67.     } 
  68.     else if (iStrType == 2) 
  69.     { 
  70.         if (strlen(szInputStr) <= 1)  // 只輸入了一個(gè)字符, 直接返回 
  71.         { 
  72.             printf("%s has only one character, please check!\n", szInputStr); 
  73.             return -1;  
  74.         } 
  75.     } 
  76.   
  77.     // 判斷輸入的字符串是否為回文串 
  78.     JudgePalindromicString(szInputStr, strlen(szInputStr), iStrType); 
  79.  
  80.     return 0;             
  81.  
  82.  
  83. /********************************************************************** 
  84. * 功能描述:判斷輸入的字符串是否為回文串 
  85. * 輸入?yún)?shù):pszInputStr-輸入的字符串 
  86.             iInputStrLen-輸入的字符串的長(zhǎng)度 
  87.             iStrType-輸入的字符串的類型 
  88. * 輸出參數(shù):無(wú) 
  89. * 返 回 值:無(wú) 
  90. * 其它說(shuō)明:無(wú) 
  91. * 修改日期       版本號(hào)        修改人          修改內(nèi)容 
  92. * ------------------------------------------------------------------- 
  93. * 20160222       V1.0      Zhou Zhaoxiong      創(chuàng)建 
  94. ***********************************************************************/ 
  95. void JudgePalindromicString(INT8 *pszInputStr, UINT32 iInputStrLen, UINT32 iStrType) 
  96.     UINT32 iPosFlag   = 0
  97.  
  98.     if (NULL == pszInputStr) 
  99.     { 
  100.         return; 
  101.     } 
  102.  
  103.     if (iStrType == 1)     // 中文字符串 
  104.     { 
  105.         for (iPosFlag = 0; iPosFlag < giCharNum/2; iPosFlag ++) 
  106.         { 
  107.             if (strcmp(gszStrCharArray[iPosFlag], gszStrCharArray[giCharNum-1-iPosFlag]) != 0)   // 有對(duì)應(yīng)字符不相等 
  108.             { 
  109.                 printf("%s is not a palindromic string!\n", pszInputStr); 
  110.                 return; 
  111.             } 
  112.         } 
  113.     } 
  114.  
  115.     if (iStrType == 2)     // 非中文字符串 
  116.     { 
  117.         // 從中間分開, 一前一后兩個(gè)字符互相比較, 如果全部對(duì)應(yīng)相等, 則是回文串 
  118.         for (iPosFlag = 0; iPosFlag < iInputStrLen/2; iPosFlag ++) 
  119.         { 
  120.             if (pszInputStr[iPosFlag] != pszInputStr[iInputStrLen-1-iPosFlag])   // 有對(duì)應(yīng)字符不相等 
  121.             { 
  122.                 printf("%s is not a palindromic string!\n", pszInputStr); 
  123.                 return; 
  124.             } 
  125.         } 
  126.     } 
  127.  
  128.     printf("%s is a palindromic string!\n", pszInputStr); 
  129.  
  130.     return; 
  131.  
  132.  
  133. /********************************************************************** 
  134. * 功能描述:獲取輸入的字符串中的每個(gè)中文字符 
  135. * 輸入?yún)?shù):pszInputStr-輸入的字符串 
  136.             iInputStrLen-輸入的字符串的長(zhǎng)度 
  137. * 輸出參數(shù):無(wú) 
  138. * 返 回 值:無(wú) 
  139. * 其它說(shuō)明:無(wú) 
  140. * 修改日期       版本號(hào)        修改人          修改內(nèi)容 
  141. * ------------------------------------------------------------------- 
  142. * 20160222       V1.0      Zhou Zhaoxiong      創(chuàng)建 
  143. ***********************************************************************/ 
  144. void GetChineseChars(INT8 *pszInputStr) 
  145.     UINT32 iPosFlag = 0
  146.      
  147.     if (NULL == pszInputStr) 
  148.     { 
  149.         return; 
  150.     } 
  151.  
  152.     memset(gszStrCharArray, 0x00, sizeof(gszStrCharArray)); 
  153.     giCharNum = 0
  154.      
  155.     while (iPosFlag < strlen(pszInputStr)) 
  156.     { 
  157.         snprintf(gszStrCharArray[giCharNum], sizeof(gszStrCharArray[giCharNum])-1, "%c%c", pszInputStr[iPosFlag], pszInputStr[iPosFlag+1]); 
  158.  
  159.         iPosFlagiPosFlag = iPosFlag + 2;    // 每個(gè)中文字符占兩個(gè)字節(jié) 
  160.          
  161.         giCharNum ++; 
  162.     } 
  163.  
  164.  
  165. /********************************************************************** 
  166. * 功能描述:判斷輸入的字符串的格式是否正確 
  167. * 輸入?yún)?shù):pszInputStr-輸入的字符串 
  168.             iStrType-輸入的字符串的類型 
  169. * 輸出參數(shù):無(wú) 
  170. * 返 回 值:0-格式正確 其它-格式不正確 
  171. * 其它說(shuō)明:無(wú) 
  172. * 修改日期       版本號(hào)        修改人          修改內(nèi)容 
  173. * ------------------------------------------------------------------- 
  174. * 20160222       V1.0      Zhou Zhaoxiong      創(chuàng)建 
  175. ***********************************************************************/ 
  176. INT32 JudgeStrFormat(INT8 *pszInputStr, UINT32 iStrType) 
  177.     UINT32 iPosFlag  = 0
  178.      
  179.     if (NULL == pszInputStr) 
  180.     { 
  181.         return -1; 
  182.     } 
  183.  
  184.     if (iStrType == 1)    // 先判斷中文字符串 
  185.     { 
  186.         for (iPosFlag = 0; iPosFlag < strlen(pszInputStr); iPosFlag ++) 
  187.         { 
  188.             if (pszInputStr[iPosFlag] >= 0)     // 不小于0則表示含有非中文字符 
  189.             { 
  190.                 printf("%s has non-Chinese character, please check!\n", pszInputStr); 
  191.                 return -2; 
  192.             } 
  193.         } 
  194.     } 
  195.     else if (iStrType == 2)    // 再判斷非中文字符串 
  196.     { 
  197.         for (iPosFlag = 0; iPosFlag < strlen(pszInputStr); iPosFlag ++) 
  198.         { 
  199.             if (pszInputStr[iPosFlag] < 0)     // 小于0則表示含有中文字符 
  200.             { 
  201.                 printf("%s has Chinese character, please check!\n", pszInputStr); 
  202.                 return -3; 
  203.             } 
  204.         } 
  205.     } 
  206.     else 
  207.     { 
  208.         printf("Please input the right string type!\n"); 
  209.         return -4;  
  210.     } 
  211.  
  212.     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)行情況如下:

  1. Please input the string type(1:中文字符串,2:非中文字符串): 
  2. Please input the string: 
  3. 人上人 
  4. 人上人 is a palindromic string! 

2.輸入中文字符串為“我是誰(shuí)”時(shí),程序運(yùn)行情況如下:

  1. Please input the string type(1:中文字符串,2:非中文字符串): 
  2. Please input the string: 
  3. 我是誰(shuí) 
  4. 我是誰(shuí) is not a palindromic string! 

3.輸入非中文字符串為“level”時(shí),程序運(yùn)行情況如下:

  1. Please input the string type(1:中文字符串,2:非中文字符串): 
  2. Please input the string: 
  3. level 
  4. level is a palindromic string! 

4.輸入非中文字符串為“good”時(shí),程序運(yùn)行情況如下:

  1. Please input the string type(1:中文字符串,2:非中文字符串): 
  2. Please input the string: 
  3. good 
  4. good is not a palindromic string! 

5.輸入字符串為“你好good”時(shí),程序運(yùn)行情況如下:

  1. Please input the string type(1:中文字符串,2:非中文字符串): 
  2. Please input the string: 
  3. 你好good 
  4. 你好good has non-Chinese character, pleasecheck! 

6.輸入字符串為“good好”時(shí),程序運(yùn)行情況如下:

  1. Please input the string type(1:中文字符串,2:非中文字符串): 
  2. Please input the string: 
  3. good好 
  4. good好 has Chinese character, pleasecheck! 

7.輸入字符串類型非1或2時(shí),程序運(yùn)行情況如下:

  1. Please input the string type(1:中文字符串,2:非中文字符串): 
  2. Please input the string: 
  3. goog 
  4. 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)】

戳這里,看該作者更多好文

責(zé)任編輯:趙寧寧 來(lái)源: 51CTO專欄
相關(guān)推薦

2016-12-30 13:32:24

字符串算法代碼

2016-12-30 13:16:51

字符串算法代碼

2016-12-29 15:58:00

字符串子串算法

2016-12-29 17:07:59

字符算法代碼

2016-12-30 13:37:50

字符串算法代碼

2016-12-29 16:25:32

字符串算法代碼

2023-02-26 22:33:32

字符串排列算法

2009-08-11 10:26:49

C#算法C#字符串反轉(zhuǎn)

2013-05-06 10:54:08

字符串字符串匹配KMP算法

2023-12-15 10:27:01

暴力匹配算法Python字符串

2021-11-12 09:44:03

字符串算法復(fù)雜度

2021-09-03 09:41:36

字符串時(shí)間復(fù)雜度

2023-04-11 08:54:57

字符串匹配算法

2013-05-06 10:49:21

Boyer-Moore算法字符串匹配

2021-09-10 08:31:54

翻轉(zhuǎn)字符串單詞

2023-08-29 15:32:57

深度學(xué)習(xí)人工智能

2024-07-03 11:23:14

2009-09-02 15:53:27

C#判斷字符串應(yīng)用

2018-07-27 08:39:44

負(fù)載均衡算法實(shí)現(xiàn)

2010-11-26 09:51:54

MySQL字符串
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)