2、过滤字符串中重复字符

本文探讨了两个C/C++程序,一个使用字符数组实现字符串去重并计数,另一个通过字符数组统计输入字符串中每个字符的出现频率。展示了字符串操作的基础技巧和字符数组在信息技术中的应用。

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

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <ctype.h>
#include <pthread.h>

int main(int argc, char** argv)
{
    char a[101] = {0};
    char b[27] = {0};
    int i, j, k, l;
    while(scanf("%s", a) != EOF)
    {   
        l = 0;
        for(i=0; i<strlen(a); i++)
        {
            k = strlen(b);
            if(k)
            {
                for(j=0; j<k; j++)
                {
                    if(tolower(a[i]) == b[j])
                    {
                        l = 1;
                        break;
                    }
                }
                if(l == 0)
                  b[k] = tolower(a[i]);
            }
            else
            {
                b[0] = tolower(a[i]);   
            }
        }
        printf("%s\n", b); 
        memset(a, 0, sizeof(a));
        memset(b, 0, sizeof(b));
    }   
	return 0;
}
#include <stdio.h>
int main()
{
	char str[101];
	while (scanf("%s", &str) != EOF)
	{
		int a[256] = { 0 };
		for (int i = 0; str[i] != '\0'; i++)
		{		
			a[str[i]] ++;
			if (a[str[i]] == 1)
				printf("%c", str[i]);
		}
		printf("\n");
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值