写的一个有关anangrams的程序,
用于检验两个单词是不是含有相同字母的(可以顺序不同)。
总体的思路是,如果两个单词的长度一样,那么就把其中的字幕按照子目标的顺序排列,
如果两个单词排列得到的结果相同,即返回“正确”,否则,则是“错误”
程序如下:include<iostream>
using namespace std;int compare(char list1[20],char list2[20],int count)  //检验函数
{    int i=0,j=0,n=0;

for(i=count;i>=1;i--)                     //第一个单词冒泡排序
{
int currentMaxIndex1=0;
        char currentMax1=list1[0];

for(j=1;j<=i;j++)
{
if(currentMax1<list1[j])
{
currentMax1=list1[j];
currentMaxIndex1=j;
}
} if(currentMaxIndex1!=i)
{
list1[currentMaxIndex1]=list1[i];
list1[i]=currentMax1;
}
}
for(i=count;i>=1;i--)                              //第二个单词,冒泡排序
{
int currentMaxIndex2=0;
        char currentMax2=list2[0];

for(j=1;j<=i;j++)
{
if(currentMax2<list2[j])
{
currentMax2=list2[j];
currentMaxIndex2=j;
}
} if(currentMaxIndex2!=i)
{
list2[currentMaxIndex2]=list2[i];
list2[i]=currentMax2;
}
} for(i=0;i<count;i++)
{
if(list1[i]==list2[i])
n++;
else
{
return 0;
break;
}
} if(n==count)
return 1;
else
return 0;}int main()
{
char list1[20]={0},list2[20]={0};
int i,count1=0,count2=0,result; cout<<"please input the first word:"<<endl;
cin.getline(list1,20);
cout<<"the first word is:"<<endl;
for (i=0;i<sizeof(list1);i++)
{
cout<<list1[i];
count1++;
}
cout<<endl;
    cout<<"please input the second word:"<<endl;
cin.getline(list2,20);
cout<<"the second word is:"<<endl;
for (i=0;i<sizeof(list2);i++)
{
cout<<list2[i];
count2++;
}
cout<<endl; if (count1==count2)
{
result=compare(list1,list2,count1);
if(result==1)
cout<<"the two words are anagrams ^^"<<endl;
else
cout<<"the two words are not anagrams!"<<endl;
}
else 
cout<<"the two words are not anagrams!"; return 0;
}
这个程序编译的时候没有问题,运行的时候,也能达到目的,但是运行时会出这样的问题:Run-Time Check Failure #2 - Stack around the variable 'list2' was corrupted.请问这是怎么回事呀,
谢谢各位大侠了~。