字符串求每一个字符出现的次数-哈希表方式实现-C描述

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zuiaisha1/article/details/54982054
//求字符串出现字符次数,求第一个次数为一的值
//哈希->哈希表概念
/* 建立一个HashTable进行映射
①HashTable的下标作为charTable 映射Key ,数组元素作为 Value

*/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>



//求字符串出现字符次数,求第一个次数为一的值
//哈希->哈希表概念
/*	建立一个HashTable进行映射
①HashTable的下标作为charTable 映射Key ,数组元素作为 Value
*/

void HashTableAlgo(char * str) {
	if (str == NULL)
		return;

	int hashTable[256] = { 0 };
	int strLength = strlen(str);
	for (int i = 0; i < strLength; i++)
	{
		hashTable[str[i]] ++;
	}
	for (size_t i = 0; i < 256; i++)
	{
		if (hashTable[i] != 0) {
			printf(" %c -> %d \n", i, hashTable[i]);
		}
	}
}

void main() {
	char rStr[] = "aaaaa651sdfsdfsd";
	printf("源字符串 -> %s \n", rStr);
	printf("检测字符串出现次数\n");
	HashTableAlgo(rStr);
	system("pause");
}



猜你喜欢

转载自blog.csdn.net/zuiaisha1/article/details/54982054