从C 中的两个字符串中找到唯一的常见单词

Find the only common word from two strings in c++

本文关键字:唯一 常见单 字符串 两个      更新时间:2023-10-16

我需要从一个函数中返回一个字符串,其中包含两个字符串的通用单词(鉴于只有一个通用单词)。我可以使用功能strcmpstrlenstrcatstrcpy。当字符串中的第一个单词相同时,我就管理了,但是如果是这样,那就不起作用。

示例不起作用:

str1[] = {"Hello world"}; //string 1
str2[] = {"Thanks you world"}; // string 2
Example that work:
char str1[] = {"Hello world"}; //string 1
char str2[] = {"Hello Danny"}; // string 2

这是我的代码:

#include <iostream>
using namespace std;
# include <string.h>
char * Strcom(char str1[], char str2[]); //declare function
int main()
{
    char str1[] = { "Hello world" }; //string 1
    char str2[] = { "Thanks you world" }; // string 2
    Strcom(str1, str2);
}
char * Strcom(char str1[], char str2[])
{
    char temp[10];
    int i = 0, z = 0;
    while (i <= strlen(str1))//run over str1, copying word in the next while loop
    {
        int k = 0;
        bool check = 0;
        while ((z < strlen(str2)) && (k < strlen(temp)))//check that I withing the lenght...
        {
            char temp[10] = { 0 };
            while ((str1[i] != ' ')&&(i<10))//copy one word to temp from str1
            {
                temp[i] = str1[i];
                i++;
            }
            if (i != strlen(temp))
            {
                temp[i] = '';
            }
            else
                temp[i-1] = '';
            i++;//to jump over ' '
            while (str2[z] != ' ') //check if its the same world (temp and str2)
            {
                if (temp[k] == str2[z])
                {
                    k++, z++;
                    check = 1;
                }
                else
                {
                    check = 0;
                    break;
                }
            }
            if (((str2[z] == ' ') || (str2[z] == '')) && (check == 1))//its the same word
            {
                temp[k] = '';//to be able to print it
                cout <<"my word is: " <<temp<< endl;
                return temp;
            }
            z += 1;
            k = 10;//to go to the outer while loop
        }
        i++;
    }
}

如果您需要C风格的C 来学习而不是生产,则可以解决所需的功能:

bool CommonWord( const char* words_a, const char* words_b, char* result )
{
    // Iterate throught the words of words_a
    while( IsolateWord( words_a, result ) )
    {
        // Start from the beginning of words_b
        const char* word_list = words_b;
        while( *word_list != '' )
        {
            // See if words match
            int eaten = 0;
            if( CompareWord( result, words_b, &eaten ) )
            {
                // Success, the matching word was copied to result before
                return true;
            }
            else
            {
                // Go to the beginning of the next word or the end of the string
                for( word_list += eaten; ( *word_list != ' ' ) && ( *word_list != '' ); word_list++ )
                {
                    ;
                }
            }
        }
    }
    // No matching words found, empty result
    result[ 0 ] = '';
    return false;
}
bool IsolateWord( const char* words, char* result )
{
    // Skip leading spaces
    const char* start = words;
    while( *start == ' ' )
    {
        start++;
    }
    // Find end of word
    const char* end = start;
    while( ( end != ' ' ) && ( end != '' ) )
    {
        end++;
    }
    // No more word
    if( end == start )
    {
        return false;
    }
    // Copy word to buffer
    while( *start != '' )
    {
        *result++ = *start++;
    }
    *result = '';
    return true;
}
bool CompareWord( const char* word_one, const char* word_list, int* eaten )
{
    // Go until characters differ or strings end
    for( *eaten = 0; ( word_one[ *eaten ] == word_list[ *eaten ] ) && ( word_one[ *eaten ] != '' ); ( *eaten )++ )
    {
        ;
    }
    // If reached end of word_one, and word_list contains the same characters plus a space or '', then the word matches
    if( ( word_one[ *eaten ] == '' ) && ( ( word_list[ *eaten ] == ' ' ) ||  ( word_list[ *eaten ] == '' ) ))
    {
        return true;
    }
    else
    {
        return false;
    }
}

警告:我手头上没有C 编译器,所以我什至没有编译上述。