C语言实现字符串中删除字符中重复的字符

本文介绍了一个使用C语言编写的程序,该程序能够统计字符串中每个字符出现的次数,并进行去重处理。通过构建一个结构体数组来记录每个字符及其对应的出现频率,最后输出处理后的字符序列。
}*/
#include<stdio.h>
#include<string.h>
int n;
char str[100];
struct words
{
	char word;
	int number;
}wordstable[100];//wordstable[100] = {{ 'a',1 }, { 'd',2 }};


int tw(char str[100], char s);
int tw(char str[100], char s)
{
	int  i;
	int count = 0;
	for (i = 0; i < n; i++)
	{
		if (str[i] == s)
			count+=1;
	}
	return count;
}
void tablew(char str[100])
{
	int i,j;
	for (i = 0; i < n; i++)
	{
		wordstable[i].word = str[i];
		wordstable[i].number = tw(str, str[i]);
		printf("%c,%d\n", wordstable[i].word, wordstable[i].number);
	}
	for (i = 0; i < n; i++)
		for (j = i+1; j < n; j++)
			if (wordstable[i].word == wordstable[j].word)
			{
				wordstable[j].word = wordstable[j + 1].word;
				wordstable[j].number = wordstable[j + 1].number;
				n--;
				j--;
			}
}

void main()
{
	printf("input str:\n");
	scanf_s("%s", str, 100);
	void tablew(char str[100]);
	int i,j;
	n = strlen(str);
	char str2[100];
	tablew(str);
	for (i = 0; i < n; i++)
	{
		printf("%c\t%d\n", wordstable[i].word, wordstable[i].number);
	}
	i = 0;
	while (wordstable[i].word != '\0')
	{
		str2[i] = wordstable[i].word;
		i++;
	}
	str2[n] = '\0';
	puts(str2);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值