在现实情况中,我们往往要从一个长字符串后中(字符中用空格隔开),找出其中的每个单词。然后在作一系列的处理,比如找出某个单词等。
一)从字符串中找出每个单词
#include <stdio.h> #include <string.h> inline char *DeleteSpace(char *pString) { while(' ' == *pString) pString ++; return pString; } int GetNumOfWords(char* pString) { if(NULL == pString) return 0; unsigned int nCount = 0; char *pSrc = pString; pSrc = DeleteSpace( pSrc ); while('\0' != *pSrc) { pSrc = strstr(pSrc," "); ++ nCount; if(NULL != pSrc) pSrc = DeleteSpace( pSrc ); else break;//the end of the string } return nCount; } void SetStringArray(char* pSrc, char **pDst) { if(NULL == pSrc) return; unsigned int nLen, nIndex = 0; char *pBegin = pSrc; char *pEnd = pSrc; pBegin = DeleteSpace( pBegin ); while('\0' != *pBegin) { pEnd = strstr(pBegin," "); if(NULL != pEnd) { nLen = pEnd - pBegin; pDst[ nIndex ] = new char[nLen + 1]; strncpy(pDst[ nIndex ], pBegin, nLen); pDst[nIndex][nLen] = '\0'; ++ nIndex; pBegin = DeleteSpace( pEnd ); } else { //the end of string. nLen = strlen(pBegin); pDst[ nIndex ] = new char[nLen + 1]; strncpy(pDst[ nIndex ], pBegin, nLen); pDst[nIndex][nLen] = '\0'; break; } } } int main() { unsigned int i, nCount; char *pSrc = " Hello. I am a student. Welcome to Huazhong University of Science and Techonology. "; nCount = GetNumOfWords(pSrc); char **pDst = new char *[ nCount ](); SetStringArray(pSrc, pDst); //Print the num of words printf("nCount = %d\n",nCount); //Print the every words for(i = 0; i < nCount; ++ i) { printf("%s\n",pDst[i]); } //free memory for(i = 0; i < nCount; ++ i) { delete [] pDst[i]; } pDst = NULL; return 0; }
二)从字符串中找出某个特定的单词
#include <stdio.h> #include <string.h> inline char *DeleteSpace(char *pString) { while(' ' == *pString) pString ++; return pString; } int GetNumOfWords(char* pString) { if(NULL == pString) return 0; unsigned int nCount = 0; char *pSrc = pString; pSrc = DeleteSpace( pSrc ); while('\0' != *pSrc) { pSrc = strstr(pSrc," "); ++ nCount; if(NULL != pSrc) pSrc = DeleteSpace( pSrc ); else break;//the end of the string } return nCount; } void SetStringArray(char* pSrc, char **pDst) { if(NULL == pSrc) return; unsigned int nLen, nIndex = 0; char *pBegin = pSrc; char *pEnd = pSrc; pBegin = DeleteSpace( pBegin ); while('\0' != *pBegin) { pEnd = strstr(pBegin," "); if(NULL != pEnd) { nLen = pEnd - pBegin; pDst[ nIndex ] = new char[nLen + 1]; strncpy(pDst[ nIndex ], pBegin, nLen); pDst[nIndex][nLen] = '\0'; ++ nIndex; pBegin = DeleteSpace( pEnd ); } else { //the end of string. nLen = strlen(pBegin); pDst[ nIndex ] = new char[nLen + 1]; strncpy(pDst[ nIndex ], pBegin, nLen); pDst[nIndex][nLen] = '\0'; break; } } } /***************************************************************************** Description : 输出排序后指定的某个字符串 Prototype : void FindSubString(int SerialNumber, char** ppOutString) Input Param : SerialNumber 排序后的序列号,从1~N为按照字典从小到大的排列顺序 Output Param : *ppOutString 指针指向字符子串的首地址,字符串必须以’\0’结束。 如果SerialNumber非法,将*ppOutString指向空字符串。 Return Value : 无 *****************************************************************************/ void FindSubString(char **pDst, int SerialNumber, const int total, char** ppOutString) { if( (SerialNumber >= 1) && (SerialNumber <= total)) { *ppOutString = pDst[SerialNumber - 1]; } else { *ppOutString = NULL; } return; }; int main() { unsigned int i, nCount; char *pSrc = " Hello. I am a student. Welcome to Huazhong University of Science and Techonology. "; nCount = GetNumOfWords(pSrc); char **pDst = new char *[ nCount ](); SetStringArray(pSrc, pDst); //Print the num of words printf("nCount = %d\n",nCount); //Print the every words for(i = 0; i < nCount; ++ i) { printf("%s\n",pDst[i]); } char *pActualOutSubStr = NULL; FindSubString(pDst, 1, nCount, &pActualOutSubStr); printf("\n%s\n",pActualOutSubStr); FindSubString(pDst, 2, nCount, &pActualOutSubStr); printf("%s\n",pActualOutSubStr); FindSubString(pDst, 3, nCount, &pActualOutSubStr); printf("%s\n",pActualOutSubStr); FindSubString(pDst, 4, nCount, &pActualOutSubStr); printf("%s\n",pActualOutSubStr); FindSubString(pDst, 5, nCount, &pActualOutSubStr); printf("%s\n",pActualOutSubStr); FindSubString(pDst, 6, nCount, &pActualOutSubStr); printf("%s\n",pActualOutSubStr); FindSubString(pDst, 7, nCount, &pActualOutSubStr); printf("%s\n",pActualOutSubStr); FindSubString(pDst, 8, nCount, &pActualOutSubStr); printf("%s\n",pActualOutSubStr); FindSubString(pDst, 9, nCount, &pActualOutSubStr); printf("%s\n",pActualOutSubStr); FindSubString( pDst, -1, nCount, &pActualOutSubStr); printf("%s\n",pActualOutSubStr); //free memory for(i = 0; i < nCount; ++ i) { delete [] pDst[i]; } pDst = NULL; return 0; }