使用C检查两个字符串是否是字谜

新手上路,请多包涵

我想出的下面的程序用于检查两个字符串是否是字谜。它适用于小弦但适用于较大的弦(我试过:听过,入伍)它给了我一个“不!”

帮助 !

 #include<iostream.h>
#include<string.h>
#include<stdio.h>

int main()
{
    char str1[100], str2[100];
    gets(str1);
    gets(str2);
    int i,j;
    int n1=strlen(str1);
    int n2=strlen(str2);
    int c=0;
    if(n1!=n2)
    {
          cout<<"\nThey are not anagrams ! ";
          return 0;
    }
    else
    {
         for(i=0;i<n1;i++)
             for(j=0;j<n2;j++)
                 if(str1[i]==str2[j])
                     ++c;
    }
    if(c==n1)
        cout<<"yes ! anagram !! ";
    else
        cout<<"no ! ";

    system("pause");
    return 0;
}

原文由 user2688416 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 274
1 个回答
Well if you don't want to sort than this code will give you perfect output.
#include <iostream>
using namespace std;
int main(){
string a="gf da";
string b="da gf";
int al,bl;
int counter =0;
al =a.length();
bl =b.length();
for(int i=0 ;i<al;i++){
    for(int j=0;j<bl;j++){
        if(a[i]==b[j]){
            if(j!=bl){
                b[j]=b[b.length()-counter-1];
                bl--;
                counter++;
                break;
            }else{
                bl--;
                counter++;
            }

        }
    }
}
if(counter==al){
    cout<<"true";
}
else{
    cout<<"false";
}
return 0;

}

原文由 Nauman Aziz 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题