输入一个字符串,单词以逗号或空格隔开。要求:滤除相同的字符串,单词以空格隔开。

本文介绍了一段使用C语言实现的字符串过滤与去重的代码,通过解析输入字符串并去除重复项,展示了字符串处理的基本技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<stdio.h>
#include<string.h>
void strFliter(char *pInput)  
{  
    char *str;
<span style="white-space:pre">	</span>char *input = pInput;
<span style="white-space:pre">	</span>char *result[256];
<span style="white-space:pre">	</span>int cnt = 0,i,j;
<span style="white-space:pre">	</span>char *temp;
<span style="white-space:pre">	</span>while(input){
<span style="white-space:pre">		</span>str = strsep(&input,",");
<span style="white-space:pre">		</span>result[cnt++] = str;
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>printf("cnt = %d\n",cnt);
<span style="white-space:pre">	</span>printf("Fliter pre: ");
<span style="white-space:pre">	</span>for(i=0;i<cnt;++i)
<span style="white-space:pre">		</span>printf("%s ",result[i]);
<span style="white-space:pre">	</span>printf("\n");
<span style="white-space:pre">	</span>for(i=0;i<cnt;){
<span style="white-space:pre">		</span>while(result[i] == NULL)
<span style="white-space:pre">			</span>++i;
<span style="white-space:pre">		</span>temp = result[i];
<span style="white-space:pre">		</span>for(j=i+1;j<cnt;){
<span style="white-space:pre">			</span>while(result[j] == NULL)
<span style="white-space:pre">			</span>++j;
<span style="white-space:pre">			</span>if(!strcmp(temp,result[j]))
<span style="white-space:pre">				</span>result[j] = NULL;
<span style="white-space:pre">			</span>++j;
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>++i;
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>printf("Fliter aft: ");
<span style="white-space:pre">	</span>for(i=0;i<cnt;++i){
<span style="white-space:pre">		</span>if(result[i]!=NULL)
<span style="white-space:pre">			</span>printf("%s ",result[i]);
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>printf("\n");
}  
  
  
  
int main(int argc, char* argv[])  
{  
  
    char inputchar[1000]={'\0'}; 
<span style="white-space:pre">	</span>scanf("%s",inputchar);
    strFliter(inputchar);   
    return 0;  
}  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值