Hi jijo!
1. I think @kwb's reply was quite useful, and about the " *(str+i) and str[i]" thing, i think he/she was right, the compiler will automaticlly add different number to the pointer according to the size of the pointer's type. It's not that stupid (:
2. I runned your program and fount out that your program does not only have problem with consecutive characters but also have problem with any string that has a substr with reversed alphabets. e.g. "wc" , in alphabets, 'c' stand before 'w', and then the program goes wrong.
This problem can be solved by adding a little bit of code in the addString() func. Like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
for(int i=tot-1;i>=0;i--)
{
if(strcmp(stLst[i],stra)==0)
return 0;
else if(strcmp(stra,stLst[i])<=0)
{
strcpy(stLst[i+1],stLst[i]);
if (i==0)
{
strcpy(stLst[i],stra);
stLst[tot+1]=(char*)malloc(2000*sizeof(char));
return 1;
}
}
else
{
strcpy(stLst[i+1],stra);
stLst[tot+1]=(char*)malloc(2000*sizeof(char));
return 1;
}
|
Notice that in the for loop you set
for(int i=tot-1;i>=0;i--)
and in the code block you put the string in right place you write:
strcpy(stLst[i+1],stra);
so, the minium num i could get, is 1. But it should be 0! Now you can see where the problem lies.
3. About the problem with consecutive same alphabets.
If you input:
aaaaa
output is something like:
I noticed that the "aa"s are not duplicated string, it was produced by permute(), think about input "aaaaa", the permute will generate ONE"aa" for string "aaaaa", ONE "aa" for string "aaaa", ONE "aa" for string "aaa", and so on... and in the sort function(which works fine), all "aa"s are sorted befor "aaa" because it's shorter. Then the "moving the others right" block of code just buried other permutation. Solution i suggest is just check if this string already exists. Like this:
1 2 3 4 5 6 7 8 9 10 11
|
bool InList(char **stLst, char sizeLst, char *stra)
{
for (int i=0;i<sizeLst;++i)
{
if (strcmp(stLst[i],stra)==0)
{
return true;
}
}
return false;
}
|
1 2
|
if (InList(stLst,tot,stra))
return 0;
|
Hope this will be helpful.