字符串的所有排列 [英] All Permutations of a String

查看:87
本文介绍了字符串的所有排列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在网站上看到的代码,我对其进行了一些更改。该代码应该使用递归来给出字符串的所有排列。在网站上,他们使用和数组来取一个字符串,但我使用指针字符串初始化,但它不起作用。

这是代码

This is the code i saw on a site and i made some changes in it . The code is supposed to give all the permutation of string using recursion . There on the site ,they used and array to take a string but i used pointer string initialization but it does not work .
Here is the code

        #include<stdio.h>
        #include<stdlib.h>
        
        void swap(char* x, char* y)
        {
            	char temp ;
        	temp=*x ;
        	*x=*y  ;
        	*y=temp ;        
        }
        
        void  permute(char *string,int start , int end )
        {        
        	int j ;
        	if(start==end)
        		printf("%s\n",string) ;
        
        	else
        		{
        			for(j=start;j<=end;j++)
        			{
        				swap((string+start),(string+j)) ;
        				permute(string,start+1,end) ;
        				swap((string+start),(string+j)) ;
        
        			}				
        			
        		}     
        }
        
        
        int main()
        {	
        	char* check;
        	*check ="ABC" ;
        	permute(check,0,2) ;
        	
        
	return 0 ;
}

推荐答案

只需说

Just say
check ="ABC";



而不是* check =ABC。这可能会起作用。


instead of "*check = "ABC". That will probably do the job.


ABC是一个常量字符串 - 你不能改变它的值或它包含的任何字符的值。



你需要分配足够的空间,并复制这可能是他们在原始版本中使用数组的原因......
"ABC" is a constant string - you can't change its value, or the value of any of the characters it contains.

You need to allocate sufficient space, and copy the string into that. That is probably the reason they used an array in the original version...


这篇关于字符串的所有排列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆