从字符串中分离找出每个单词,并且寻找某个特定的单词

在现实情况中,我们往往要从一个长字符串后中(字符中用空格隔开),找出其中的每个单词。然后在作一系列的处理,比如找出某个单词等。


一)从字符串中找出每个单词

#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;
}



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值