输入一个字符串,打印出该字符串中字符的所有排列

算法描述:

输入一个字符串,打印出该字符串中字符的所有排列

把一个字符串看成两部分组成:第一部分为它的第一个字符,第二部分是后面的所有字符。

算法实现:

/*************************************************************************
	> File Name: main.c
	> Author: cyf
	> Mail: [email protected]
	> Created Time: 2016年04月27日 星期三 09时00分26秒
 ************************************************************************/

#include "permutation.h"

int main()
{
	char buff[] = "abc";
	permutation(buff, buff);
	return 0;
}

/*************************************************************************
	> File Name: permutation.h
	> Author: cyf
	> Mail: [email protected]
	> Created Time: 2016年04月27日 星期三 08时54分24秒
 ************************************************************************/

#ifndef _PERMUTATION_H
#define _PERMUTATION_H

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

void permutation(char *pStr, char *pBegin);


#endif


/*************************************************************************
	> File Name: permutation.c
	> Author: cyf
	> Mail: [email protected]
	> Created Time: 2016年04月27日 星期三 08时59分11秒
 ************************************************************************/

#include "permutation.h"
/*
 * 输入一个字符串,打印出该字符串中字符的所有排列
 * */
void permutation(char *pStr, char *pBegin)
{
	if (pStr == NULL || pBegin == NULL)
		return;

	if (*pBegin == '\0')
	{
		printf("%s\n", pStr);
	}
	else
	{
		char *pChar = pBegin;
		for (pChar = pBegin; *pChar != '\0'; ++pChar)
		{
			char tmp = *pChar;
			*pChar = *pBegin;
			*pBegin = tmp;
			permutation(pStr, pBegin + 1);
			tmp = *pChar;
			*pChar = *pBegin;
			*pBegin = tmp;
		}
	}
}

CC = gcc
CFLAGS = -g -O2 -Wall

%.o:%.c
	$(CC) -o $@ -c $(CFLAGS) $<

main:main.o permutation.o
	$(CC) main.o permutation.o -o main $(CFLAGS)

clean:
	rm -rf *.o main



你可能感兴趣的:(输入一个字符串,打印出该字符串中字符的所有排列)