Ryan's WorkSpace
  • 首页
  • 关于我
  1. 首页
  2. 通用分类
  3. 正文

10.32查找仅有一个的字符

2016年11月01日 1001点热度 0人点赞 0条评论

内容纲要
[hide][code lang="c"] #include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define tableSize 256 //创建一个哈希表,因为ASCII码表中只有0~255共256个字符。

char First_Char(char* pString)
{
if (!pString) //输入不合法
return 0;
int hashTable[tableSize];
for (int i = 0; i < tableSize; i++)
hashTable[i] = 0;
//确定字符串中每个字符出现的次数
char* pHashKey = pString;
while (*(pHashKey) != '\0')
hashTable[*(pHashKey++)]++;
//找到字符串中只出现一次的那个字符
pHashKey = pString;
while (*pHashKey != '\0')
{
if (hashTable[*pHashKey] == 1)
return*pHashKey;
pHashKey++;
}
//如果这个字符串为空,或者字符串中的每个字符都至少出现两次
return 0;
}

int main(void)
{
char str[1000]; //这个函数是在字符串特别大时建议使用,故定义一个大小为1000的数组
int a,i;
scanf("%d",&a);
for (i = 0; i < a; i++) {
scanf("%s",str);
if (First_Char(str) == 0)
printf("NO\n");
else
printf("%c\n", First_Char(str));
}
return 0;
}
[/code] [/hide]

相关

本作品采用 知识共享署名 4.0 国际许可协议 进行许可
标签: C
最后更新:2016年11月01日

Ryan Lee

如果帮助到你,请点击广告,谢谢!

点赞
< 上一篇
下一篇 >

文章评论

您需要 登录 之后才可以评论

如果帮助到你,请点击广告,谢谢!

用户您好!请先登录!
登录 注册
Social Media
Github: ryanlee2014
标签聚合
JavaScript php C++ Java hustoj C Apache GitHub
友链
Pacolyon
Lucien's blog
Slian's DreamWork
卡拉搜索
  • 0
  • 15,321
  • 5,558
  • 0
广告

COPYRIGHT © 2020 Ryan's WorkSpace. ALL RIGHTS RESERVED.

THEME KRATOS MADE BY VTROIS

登录
注册|忘记密码?