Anagram检测方法C .将字符串转换为ASCII值的问题

Anagram detection method c++. Problems converting string to ascii value

本文关键字:ASCII 问题 转换 字符串 检测 方法 Anagram      更新时间:2023-10-16

对于可能能够帮助我解决这个问题的任何人。我正在创建一种方法,该方法将比较两个字符串并检测它们是否是Anagram。Anagram是两个字符串,具有相同的字母,尽管它们的顺序可能不同。例如,"听"answers" Iltsen"是Anagrams。

我决定将琴弦分解成char阵列。我知道这是正确工作的,因为我使用每个数组元素上的COUT对其进行了测试。接下来是出错的地方。我尝试使用每个字符的ASCII值,并将其添加到每个数组的变量中。这意味着,如果值匹配,则必须是一个字符。

但是,无论出于何种未知原因,它都无法正常工作。我发现它正在读取索引0两次,而不是另一个数组。我很困惑。我绝对不知道这发生了什么。我尝试了多种不同的解决方案,没有运气发现问题。如果有人知道这里发生了什么,我将非常感谢您的帮助。

谢谢!

#include "stdafx.h"
#include <iostream>
#include <string>
#include <math.h>
#include <iomanip>
#include <cctype>
#include <vector>
using namespace std;
bool isAnagram(string s1,string s2)
{
static char firstString[] = { 'c' };
static char secondString[] = { 'c' };
int size = s1.length();
static int count1 = 0;
static int count2 = 0; 
cout << s1 << endl;
cout << s2 << endl;
if (s1.length() == s2.length())
{
    for (int i = 0; i < size; i++)
    {
        firstString[i] = s1.at(i);
        cout << i;
    }
    for (int i = 0; i < size; i++)
    {
        secondString[i] = s2.at(i);
        cout << i;
    }
    cout << endl;
    for (int i = 0; i < size; i++)
    {
        count1 = count1 + (int)firstString[i];
        cout << "first" << i << ": " << firstString[i] << " = " <<         (int)firstString[i] << endl;
        count2 = count2 + (int)secondString[i];
        cout << "second" << i << ": " << secondString[i] << " = " << (int)secondString[i] << endl;
    }
    cout << count1 << " and " << count2 << endl;
    if (count1 == count2)
        return true;
}
else
    return false;
count1 = 0;
count2 = 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
static char end;
do
{
    string s1;
    string s2;
    cout << "Please enter the first string: ";
    cin >> s1;
    cout << endl << "Please enter the second string: ";
    cin >> s2;
    bool result = isAnagram(s1, s2);
    static string resultString;
    if (result == true)
        resultString = "True";
    else
        resultString = "False";
    cout << endl << "Anagram test result: " << resultString << endl;
    cout << endl << "enter E for end or any other key to run again: ";
    cin >> end;
    cout << "----------------------------------------" << endl;
} while (end != 'e' && end != 'E');
return 0;
} 

在您的情况下使用静态变量无用,没有它们,您将不需要isAnagram的最后2行。将这两个字符串存储在char阵列中也没有用,因为您可以将它们直接在第三个循环中使用(您也可以溢出缓冲区,大小为1)

for (int i = 0; i < size; i++)
{
    std::cout << count1 << " ";
    count1 = count1 + (int)s1.at(i);
    cout << "first" << i << ": " << s1.at(i) << " = " << (int)s1.at(i) << endl;
    count2 = count2 + (int)s2.at(i);
    cout << "second" << i << ": " << s2.at(i) << " = " << (int)s2.at(i) << endl;
}

您也不能说2个字符串确实包含相同的字母来通过比较其ASCII值的总和,就像说3 4与2 5相同,因为两者都给出7。您可以创建一个52 int s的数组,每个元素都是其字母的计数器,然后您可以用一个循环循环在两个字符串上循环,其中第一个字符串的每个字母在数组中递增其元素,第二个字符串字母为减少元素。

if (s1.length() != s2.length())
    return false;
std::vector<int> counts(52);
for (unsigned int i = 0; i < s1.length(); ++i)
{
    ++counts[s1[i] - (s1[i] < 91 ? 65 : 71)];
    --counts[s2[i] - (s2[i] < 91 ? 65 : 71)];
}

确保数组初始化为0。最后,您需要在数组上循环,如果其中一个元素不是0,那不是字段,否则返回true。

for (unsigned int i = 0; i < 52; ++i)
    if (counts[i] != 0)
        return false;
return true;