在字符串中找出第一个只出现一次的字符
2016-04-21 09:09
120 查看
算法描述:
在字符串中找出第一个只出现一次的字符
算法实现:
在字符串中找出第一个只出现一次的字符
算法实现:
/************************************************************************* > File Name: FirstNotRepeatingChar.h > Author: cyf > Mail: XXX@qq.com > Created Time: 2016年04月19日 星期二 16时00分43秒 ************************************************************************/ #ifndef _FIRSTNOTREPEATINGCHAR_H #define _FIRSTNOTREPEATINGCHAR_H #include <stdio.h> #include <stdlib.h> #define HASHSIZE 256 char FirstNotRepeatingChar(char *pStr); #endif
/************************************************************************* > File Name: FirstNotRepeatingChar.c > Author: cyf > Mail: XXX@qq.com > Created Time: 2016年04月19日 星期二 16时01分54秒 ************************************************************************/ #include "FirstNotRepeatingChar.h" char FirstNotRepeatingChar(char *pStr) { if (pStr == NULL) { return '\0'; } unsigned int hashtable[HASHSIZE] = {0}; char *p = pStr; while (*p != '\0') { hashtable[*(p++)]++; } p = pStr; while (*p != '\0') { if (hashtable[*p] == 1) { return *p; } p++; } return '\0'; }
/************************************************************************* > File Name: main.c > Author: cyf > Mail: XXX@qq.com > Created Time: 2016年04月19日 星期二 16时10分01秒 ************************************************************************/ #include "FirstNotRepeatingChar.h" void Test(char *testname, char *string, char expected) { if (FirstNotRepeatingChar(string) == expected) { printf("test %s pass!\n", testname); } else { printf("test %s fail!\n", testname); } } void Test1() { char string[] = "abaccdeff"; Test("Test1", string, 'b'); } int main() { Test1(); return 0; }
CC = gcc CFLAGS = -g -O2 -Wall %.o:%.c $(CC) -o $@ -c $(CFLAGS) $< main:main.o FirstNotRepeatingChar.o $(CC) main.o FirstNotRepeatingChar.o -o main $(CFLAGS) clean: rm -rf *.o main
相关文章推荐
- LeetCode 38. Count and Say
- 寻找一个单向链表的中项,如果存在两个则返回前一个,给出算法描述
- JQ 1.9 API在线资源
- Django提交POST表单“CSRF verification failed. Request aborted”问题的解决
- wamp服务器
- centos 关闭防火墙
- spring框架学习(六)AOP
- 将Fragment加载到布局
- [调试] 打印类名方法名行数的Log日志
- hadoop,hbase,hive,zookeeper整合可行性分析及版本确定
- UITbaleView滚动到最后一行
- 第六周项目一
- 交通运输部·车载导航系统——终端如何与服务器通信——玩转通信协议(源码下载)
- 气象数据集数据和相关源码
- spring框架学习(五)注解
- SpringMVC源码剖析(一)- 从抽象和接口说起
- 18个最好的代码编辑器/IDE工具
- 第8周项目:【项目 1 -数组作数据成员】
- SVN相关
- [hdu 5521][2015ACM/ICPC亚洲区长春站] House Building