在字符串中找出第一个只出现一次的字符

算法描述:

在字符串中找出第一个只出现一次的字符

算法实现:

/*************************************************************************
	> File Name: FirstNotRepeatingChar.h
	> Author: cyf
	> Mail: [email protected]
	> 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: [email protected]
	> 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: [email protected]
	> 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




你可能感兴趣的:(在字符串中找出第一个只出现一次的字符)