文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>小技巧--巧用ASCII的表示范围来对字符串进行处理

小技巧--巧用ASCII的表示范围来对字符串进行处理

时间:2010-08-23  来源:hlzembedded

(1)从给定的字符串中删除特定字符串中出现的字符
//通用ASCII的范围是0~127
void setcurrentchar(int cIndex[],char *pch)
{
    size_t i;
    if(pch == NULL) return;
    for(i = 0;i<strlen(pch);i++)
    {
        cIndex[pch[i]] = 1;
    }
}

//从给定的字符串中删除特定字符串中出现的字符
void deleteCurrentchar(char *pSrc,char*pch)
{
    char *pFast,*pSlow;
    int cIndex[127]={0};
    if(pSrc==NULL || pch == NULL) return;

    setcurrentchar(cIndex,pch);

    for(pSlow=pFast=pSrc;*pFast != '\0';)
    {
        if(cIndex[(int)*pFast])//如果该字符需要删除
        {
            //printf(" %c ",*pFast);
            pFast++;
        }
        else
        {
            *pSlow++ = *pFast++;
        }
    }
    *pSlow='\0';
}
(2)解析一个字符串,对字符串中重复出现的字符,只在第一次出现时保留
void setcurrentchar(int cIndex[],char ch)
{
   cIndex[ch] = 1;
}
void deleteSecondOccur(char *pSrc)
{
    int cIndex[128]={0};
    char *pF,*pS;

    if(pSrc == NULL) return;

    for(pS=pF=pSrc;*pF != '\0';)
    {
        if(cIndex[*pF])
        {
            printf("again:%c\n",*pF);
            pF++;
        }
        else
        {
            setcurrentchar(cIndex,*pF);
            *pS++=*pF++;
        }
    }
    *pS='\0';
}
相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载