成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

【字符串處理算法】字符串包含的算法設計及C代碼實現

開發 開發工具 算法
今天講一講字符串包含的算法設計及C代碼實現。

一、需求描述

給定一個長字符串和一個短字符串,編寫程序判斷短字符串中的所有字符是否都在長字符串中。如果是,則長字符串包含短字符串;反之,不包含。

為了盡量包含大多數情況,字符串中可以包含大小寫英文字母、數字和各種標點符號,并且區分大小寫字母。

下面舉幾個例子予以說明:

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

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

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

[[180306]]

二、算法設計

我們都知道,就像人體是由一個個的細胞組成一樣,一個字符串是由一個個的字符組成。如果組成某個字符串的所有字符都在另一個字符串中,那么這個字符串就被另一個字符串包含。

因此,我們可以考慮先將兩個字符串中的所有字符都找出來,再判斷較短的字符串中的所有字符是否都出現在了較長的字符串中。如果是,那么兩個字符串是包含與被包含的關系;如果不是,那么兩個字符串則“形同陌路”。

程序的總體流程如圖1所示。

圖1 程序的總體流程

三、特殊流程考慮

在編寫程序的過程中,我們要對輸入的兩個字符串的長度及格式多做考慮,如:

1.如果輸入失誤,導致短字符串的長度大于了長字符串,那么程序直接返回,不進行后續處理。

2.不允許在輸入字符串的中間出現空格,如果出現了,只能把空格前面的內容作為輸入的字符串。

3.輸入字符串可以包含字母(區分大小寫)、數字、標點符號等字符。

4.為了方便程序處理,設定較長的字符串最長為500個字節,較短的字符串最長為100個字節。

四、程序代碼

  1. /********************************************************************** 
  2. * 版權所有 (C)2016, Zhou Zhaoxiong。 
  3. * 文件名稱: StringContains.c 
  4. * 文件標識: 無 
  5. * 內容摘要: 測試一個字符串是否是另一個字符串的子串 
  6. * 其它說明: 例如, "ABC"是"ABCD"的子串 
  7. * 當前版本: V1.0 
  8. * 作    者: Zhou Zhaoxiong 
  9. * 完成日期: 20160216 
  10. **********************************************************************/ 
  11. #include <stdio.h> 
  12. #include <stdlib.h> 
  13.  
  14. // 重新定義數據類型 
  15. typedef signed   char       INT8; 
  16. typedef unsigned short int  UINT16; 
  17. typedef          int        INT32; 
  18. typedef unsigned int        UINT32; 
  19.  
  20. // 存放字符串中的字符和格式的結構體 
  21. typedef struct 
  22.     INT8   szStrCharArray[101][2];     // 字符串中不相同的字符的存放數組,***支持100個 
  23.     INT32  iStrCharCount;              // 字符串中不相同的字符的個數 
  24. } StrInfo_T; 
  25.  
  26. StrInfo_T gtLongerStrInfo  = {0}; 
  27. StrInfo_T gtShorterStrInfo = {0}; 
  28.  
  29.  
  30. // 函數聲明 
  31. void GetStrChar(INT8 *pszInputStr, INT32 iProcessFlag); 
  32. INT32 JudgeIfContainsStr(); 
  33.  
  34.  
  35. /********************************************************************** 
  36. * 功能描述: 主函數 
  37. * 輸入參數: 無 
  38. * 輸出參數: 無 
  39. * 返 回 值: 0-執行成功   其它-執行失敗 
  40. * 其它說明: 無 
  41. * 修改日期        版本號       修改人          修改內容 
  42. * ----------------------------------------------------------------- 
  43. * 20160216        V1.0     Zhou Zhaoxiong        創建 
  44. ***********************************************************************/ 
  45. INT32 main() 
  46.     INT8   szLongerStr[500]   = {0}; 
  47.     INT8   szShorterStr[100]  = {0}; 
  48.      
  49.     UINT32 iContainFlag = 1;     // 包含標志, 1-包含, 0-不包含 
  50.      
  51.     printf("Please input the longer string: \n"); 
  52.     scanf("%s", szLongerStr); 
  53.     printf("LongerStr=%s\n", szLongerStr); 
  54.  
  55.     printf("Please input the shorter string: \n"); 
  56.     scanf("%s", szShorterStr); 
  57.     printf("ShorterStr=%s\n", szShorterStr); 
  58.  
  59.     // 如果ShorterStr的長度大于LongerStr, 則直接返回 
  60.     if (strlen(szShorterStr) > strlen(szLongerStr)) 
  61.     { 
  62.         printf("%s is longer than %s, please check!\n", szShorterStr, szLongerStr); 
  63.         return -1; 
  64.     } 
  65.      
  66.     // 獲取較長的字符串中的不同的字符 
  67.     GetStrChar(szLongerStr, 1); 
  68.  
  69.     // 獲取較短的字符串中的不同的字符 
  70.     GetStrChar(szShorterStr, 2); 
  71.  
  72.     iContainFlag = JudgeIfContainsStr(); 
  73.     if (iContainFlag == 0) 
  74.     { 
  75.         printf("%s doesn't contain %s!\n", szLongerStr, szShorterStr); 
  76.     } 
  77.     else 
  78.     { 
  79.         printf("%s contains %s!\n", szLongerStr, szShorterStr); 
  80.     } 
  81.      
  82.     return 0;             
  83.  
  84.  
  85. /********************************************************************** 
  86. * 功能描述: 獲取字符串中不相同的字符及其個數 
  87. * 輸入參數: pszInputStr-輸入字符串 
  88.              iProcessFlag-處理標志(1:處理長字符串, 2:處理短字符串) 
  89. * 輸出參數: 無 
  90. * 返 回 值: 無 
  91. * 其它說明: 無 
  92. * 修改日期          版本號         修改人           修改內容 
  93. * --------------------------------------------------------------- 
  94. * 20160216          V1.0       Zhou Zhaoxiong        創建 
  95. ***********************************************************************/ 
  96. void GetStrChar(INT8 *pszInputStr, INT32 iProcessFlag) 
  97.     INT32  iCharCount      = 0;                // 字符個數 
  98.     INT8   szInputStr[501] = {0}; 
  99.     INT8   szCharBuf[2]    = {0};              // 存放單個字符的緩存 
  100.     INT32  iRepeatFlag     = 0
  101.     UINT32 iStrPosFlag     = 0
  102.     UINT32 iLoopFlag       = 0
  103.     UINT32 iInputStrLen    = 0
  104.  
  105.     if (pszInputStr == NULL) 
  106.     { 
  107.         return; 
  108.     } 
  109.  
  110.     iInputStrLen = strlen(pszInputStr); 
  111.     if (iInputStrLen >= 500)  // ***支持100個字母 
  112.     { 
  113.         return; 
  114.     } 
  115.  
  116.     memcpy(szInputStr, pszInputStr, iInputStrLen); 
  117.  
  118.     iCharCount = 0
  119.  
  120.     for (iStrPosFlag = 0; iStrPosFlag < iInputStrLen; iStrPosFlag ++) 
  121.     { 
  122.         iRepeatFlag = 0
  123.          
  124.         // 判斷正要獲取的字符是否已經存在了 
  125.         memset(szCharBuf, 0x00, sizeof(szCharBuf)); 
  126.         memcpy(szCharBuf, szInputStr+iStrPosFlag, 1); 
  127.  
  128.         // 若與之前已經加入的字符重復, 則忽略 
  129.         for (iLoopFlag = 0; iLoopFlag < iCharCount; iLoopFlag ++) 
  130.         { 
  131.             if (iProcessFlag == 1)    // 處理長字符串 
  132.             { 
  133.                 if (0 == strncmp(gtLongerStrInfo.szStrCharArray[iLoopFlag], szCharBuf, 1)) 
  134.                 { 
  135.                     iRepeatFlag = 1;  // 有重復的, 直接忽略 
  136.                     break; 
  137.                 } 
  138.             } 
  139.             else                     // 處理短字符串 
  140.             { 
  141.                 if (0 == strncmp(gtShorterStrInfo.szStrCharArray[iLoopFlag], szCharBuf, 1)) 
  142.                 { 
  143.                     iRepeatFlag = 1;  // 有重復的, 直接忽略 
  144.                     break; 
  145.                 } 
  146.             } 
  147.         } 
  148.  
  149.         if (1 == iRepeatFlag) 
  150.         { 
  151.             continue; 
  152.         } 
  153.  
  154.         if (iProcessFlag == 1)    // 處理長字符串 
  155.         { 
  156.             strncpy(gtLongerStrInfo.szStrCharArray[iCharCount], szCharBuf, 1); 
  157.         } 
  158.         else                      // 處理短字符串 
  159.         { 
  160.             strncpy(gtShorterStrInfo.szStrCharArray[iCharCount], szCharBuf, 1); 
  161.         } 
  162.  
  163.         iCharCountiCharCount = iCharCount + 1; 
  164.     } 
  165.  
  166.     if (iProcessFlag == 1)    // 處理長字符串 
  167.     { 
  168.         gtLongerStrInfo.iStrCharCount = iCharCount
  169.     } 
  170.     else                      // 處理短字符串 
  171.     { 
  172.         gtShorterStrInfo.iStrCharCount = iCharCount
  173.     } 
  174.  
  175.     return; 
  176.  
  177.  
  178. /********************************************************************** 
  179. * 功能描述: 判斷長字符串是否包含了短字符串 
  180. * 輸入參數: 無 
  181. * 輸出參數: 無 
  182. * 返 回 值: 1-包含了 0-沒有包含 
  183. * 其它說明: 無 
  184. * 修改日期          版本號         修改人           修改內容 
  185. * --------------------------------------------------------------- 
  186. * 20160216          V1.0       Zhou Zhaoxiong        創建 
  187. ***********************************************************************/ 
  188. INT32 JudgeIfContainsStr() 
  189.     UINT32 iLongerLoopFlag    = 0
  190.     UINT32 iShorterLoopFlag   = 0
  191.     UINT32 iCharIdenticalFlag = 0
  192.  
  193.     // 判斷較短的字符串中的字符是否全部都在較長的字符串中的字符中 
  194.     for (iShorterLoopFlag = 0; iShorterLoopFlag < gtShorterStrInfo.iStrCharCount; iShorterLoopFlag ++) 
  195.     { 
  196.         iCharIdenticalFlag = 0
  197.         for (iLongerLoopFlag = 0; iLongerLoopFlag < gtLongerStrInfo.iStrCharCount; iLongerLoopFlag ++) 
  198.         { 
  199.             if (strcmp(gtShorterStrInfo.szStrCharArray[iShorterLoopFlag], gtLongerStrInfo.szStrCharArray[iLongerLoopFlag]) == 0) 
  200.             { 
  201.                 iCharIdenticalFlag = 1;    // 字符相同 
  202.                 break; 
  203.             } 
  204.         } 
  205.  
  206.         if (iCharIdenticalFlag == 0)     // 表示兩個字符串中有不相同的字符 
  207.         { 
  208.             return 0; 
  209.         } 
  210.     } 
  211.  
  212.     return 1; 

五、程序測試

我們將編寫好的程序“StringContains.c”上傳到Linux機器,并使用“gcc -g -o StringContainsStringContains.c”命令對該程序進行編譯,生成“StringContains”文件。下面對程序進行詳細的測試。

1.輸入較長字符串為“ABCDF”、較短字符串為“AF”時,程序運行情況如下:

  1. Please input the longer string: 
  2. ABCDF 
  3. LongerStr=ABCDF 
  4. Please input the shorter string: 
  5. AF 
  6. ShorterStr=AF 
  7. ABCDF contains AF! 

2.輸入較長字符串為“AB”、較短字符串為“ABC”時,程序運行情況如下:

  1. Please input the longer string: 
  2. AB 
  3. LongerStr=AB 
  4. Please input the shorter string: 
  5. ABC 
  6. ShorterStr=ABC 
  7. ABC is longer than AB, please check! 

3.輸入較長字符串為“awe”、較短字符串為“rf”時,程序運行情況如下:

  1. Please input the longer string: 
  2. awe 
  3. LongerStr=awe 
  4. Please input the shorter string: 
  5. rf 
  6. ShorterStr=rf 
  7. awe doesn't contain rf! 

4.輸入較長字符串為“`11245”、較短字符串為“45”時,程序運行情況如下:

  1. Please input the longer string: 
  2. `11245 
  3. LongerStr=`11245 
  4. Please input the shorter string: 
  5. 45 
  6. ShorterStr=45 
  7. `11245 contains 45! 

5.輸入較長字符串為“123”、較短字符串為“123 45”時,程序運行情況如下:

  1. Please input the longer string: 
  2. 123 
  3. LongerStr=123 
  4. Please input the shorter string: 
  5. 123 45 
  6. ShorterStr=123 
  7. 123 contains 123! 

可見,對于上面考慮到的幾種特殊情況,程序均能做出正確的處理。

六、需求擴展

基于本文中的需求和程序,我們可考慮對需求進行以下擴展:

1.限制輸入的字符串中只能包含字母,如果包含了其它字符,則直接退出而不進行處理。

 

2.如果較短的字符串中的所有字符雖然都在較長的字符串中,但某個字符在較短的字符串中出現的次數大于了在較長的字符串中出現的次數,那么就認為較長的字符串不包含較短的字符串。

【本文是51CTO專欄作者周兆熊的原創文章,作者微信公眾號:周氏邏輯(logiczhou)】

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

責任編輯:趙寧寧 來源: 51CTO專欄
相關推薦

2016-12-30 13:16:51

字符串算法代碼

2016-12-29 17:14:41

回文串算法代碼

2016-12-30 13:37:50

字符串算法代碼

2016-12-29 15:58:00

字符串子串算法

2016-12-29 17:07:59

字符算法代碼

2023-02-26 22:33:32

字符串排列算法

2009-08-11 10:26:49

C#算法C#字符串反轉

2021-09-03 09:41:36

字符串時間復雜度

2013-05-06 10:54:08

字符串字符串匹配KMP算法

2023-12-15 10:27:01

暴力匹配算法Python字符串

2010-11-26 09:51:54

MySQL字符串

2023-04-11 08:54:57

字符串匹配算法

2013-05-06 10:49:21

Boyer-Moore算法字符串匹配

2021-09-10 08:31:54

翻轉字符串單詞

2016-12-29 16:25:32

字符串算法代碼

2024-07-03 11:23:14

2010-11-26 10:43:48

MySQL分割字符串

2024-01-09 16:43:49

Shell腳本開發

2010-09-09 11:48:00

SQL函數字符串

2010-07-14 16:35:52

Perl字符串處理函數
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 99精品福利视频 | 中文字幕欧美一区二区 | 国产精品一区在线观看 | 亚洲欧美另类在线 | 91日日| 日韩一区中文字幕 | 欧美高清视频一区 | 看av网址| 不卡视频一区二区三区 | 国产在线视频一区二区董小宛性色 | 国产精品视频网 | 欧美成人综合 | 国产一区二区在线观看视频 | 不卡一二三区 | 国产精品1区 | 成人性视频在线 | 一级a性色生活片久久毛片 午夜精品在线观看 | 天堂资源最新在线 | 欧美三级成人理伦 | 9999视频 | 日韩一区二区三区在线 | 第四色播日韩第一页 | 国产精品明星裸体写真集 | 精品国产91 | 男女视频免费 | 免费的一级视频 | 国产日韩精品在线 | 国产无人区一区二区三区 | 中文一区二区 | 国产精品日日夜夜 | 亚洲日本视频 | 国产成人久久精品一区二区三区 | 日韩免费av | 国产一区二区三区 | 国产日韩欧美在线观看 | 国产a爽一区二区久久久 | 午夜精品久久久久99蜜 | 精品久久久久国产免费第一页 | 韩日一区二区三区 | 国产综合视频 | 欧美 日韩 在线播放 |