确定是否有2个字符串是同义词。

问题描述 投票:0回答:2

编写一个函数,接受一个 c 字符串和一个字符作为参数。返回该字符在字符串中出现的次数。

我的程序中也要这样做。

将字符串A和B转换为小写字母。对于字符串A中的每个字母,对A和B调用函数。

这是我到目前为止的代码。

#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;

char tolower(char c) {

    return c;
}

int countChars(const char* string, char ch)
{
    int count = 0;
    int i;

    int length = strlen(string);

    for (i = 0; i < length; i++)
    {
        if (string[i] == ch)
        {
            count++;
        }
    }
    return count;
}

int main() {
    char stringA[50];
    char stringB[50];

    cout << "Please enter first string: ";
    cin.getline(stringA, 50);

    cout << "Please enter second string: ";
    cin.getline(stringB, 50);

    cout << "String 1: " << stringA << endl;
    cout << "String 2: " << stringB << endl;

    if (strcmp(stringA, stringB) == 0) {
        cout << "These strings are anagrams" << endl;
    }
    else {
        cout << "These strings are not anagrams" << endl;
    }

}
c++
2个回答
1
投票

如果你想在一个 简单的 考虑以下代码。

#include <string>
#include <iostream>
#include <algorithm>

bool is_anagram(std::string s1, std::string s2)
{
    std::sort(s1.begin(), s1.end());
    std::sort(s2.begin(), s2.end());
    return s1 == s2;
}

int main(void)
{
    std::string s1, s2;

    std::cout << "Input first string: ";
    getline(std::cin, s1);

    std::cout << "Input second string: ";
    getline(std::cin, s2);

    if (is_anagram(s1, s2))
        std::cout << "Strings are anagrams." << std::endl;
    else
        std::cout << "Strings are NOT anagrams." << std::endl;

    return 0;
}

这个函数 is_anagram() 返回 true 当排序后的字符串(用于 algorithm 库的)是一样的。

请注意,这些字符串是区分大小写的。 字符串是区分大小写的. Hellohello 是不同的。

希望对你有所帮助。 :D


0
投票

只要把两个字符串排序,然后进行比较。

sort(stringA, stringA+strlen(stringA))sort(stringB, stringB+strlen(stringB))

就在if语句之前。

你是在检查这两个字符串是否是 恰恰 同等。但异构体可以有不同的字母排列,但必须有相同的字母。

虽然你需要检查可能需要检查,如果2个字符串是相同的(如果你不认为一个字符串是自己的anagram)。

PS-谷歌搜索会立即为你提供一个答案。


0
投票

最简单的方法是计算每个字母的数量。注意,你不需要计算每个字母,只需要计算字符串中的那些字母。要比较它们的结果存储在地图中。

#include <iostream>
#include <string>
#include <map>
#include <array>

class AnagramCheck
{
    // I am going to be lazy and use map to store the counts.
    // But you can do this much smarter and get a better complexity
    // by using an array. But that involves more checks
    std::map<char, int> data;
    public:
        explicit AnagramCheck(std::string const& str)
        {
            for(auto x: str) {
                ++data[std::tolower(x)]; // If you want to be smart ignore space/punctuation
            }
        }
        bool operator==(AnagramCheck const& rhs) const
        {
            return data == rhs.data;
        }
};

int main()
{
    std::string         line1;
    std::getline(std::cin, line1);
    AnagramCheck        line1Check(line1);

    std::string         line2;
    std::getline(std::cin, line2);
    AnagramCheck        line2Check(line2);

    if (line1Check == line2Check) {
        std::cout << "Anagram\n";
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.