Delete string

//统计单词个数,并删除指定的单词
void delelteTheWord(){
	char ch[]="this a is    is   jkl rktu shj word text test";
	string delstr="text";
	int i=0,count=0,word=0,wordlength=0;
	char *p=NULL;
    while(i<strlen(ch)){
    	if(ch[i]==' '){
    		word=0;
    		printf("有 %d 长度\n",wordlength);
    		string tmpstr="";
    		for(int move=0;move<wordlength;move++){
    			tmpstr+=*(p+move);
			}
			if(!tmpstr.compare(delstr)){
				for(int move=0;move<wordlength;move++){
					*(p+move)=' ';//这里可以改进,将p之后的字符向前移动move个进行覆盖操作 
				}
			}
    		tmpstr="";
    		wordlength=0;
		}  
        else if(word==0)
        {
        	p=ch+i;
            word=1;
            count++;
            wordlength=1;
            
        }else if(word==1){
        	wordlength++;
		}
        i++;
	}
    printf("这里是最后一个单词的长度,一定要加上,不然最后一个单词的长度不能统计出来%d \n",wordlength);
    printf("内容%s\n",ch);
    printf("总共有 %d 个单词\n",count);
	
} 

 

Published 37 original articles · won praise 12 · views 6491

Guess you like

Origin blog.csdn.net/weixin_38960774/article/details/102559138