从C 中的两个字符串中找到唯一的常见单词
Find the only common word from two strings in c++
我需要从一个函数中返回一个字符串,其中包含两个字符串的通用单词(鉴于只有一个通用单词)。我可以使用功能strcmp
,strlen
,strcat
和strcpy
。当字符串中的第一个单词相同时,我就管理了,但是如果是这样,那就不起作用。
示例不起作用:
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 编译器,所以我什至没有编译上述。
相关文章:
- 何时在引用或唯一指针上使用移动语义
- C++优先级队列,按对象的唯一指针的特定方法升序排列
- QTreeView幻灯片多选后无法使用单击选择
- 计算排序向量的向量中唯一值的计数
- 如何使用Visual Studio 2017在C++中为参数化对象数组使用唯一指针
- 通过组合不同的类型来创建唯一的id
- 使用Unique_ptr确保工厂中的对象唯一
- 用常见虚拟函数实现的任意组合来实现派生类的正确方法是什么
- c++多进程编写一个唯一的文件
- 在以唯一ptr为值的C++映射中,动态内存何时会被销毁
- 为什么在单例中,检查类==空?
- 如何更改唯一指针向量的可见性
- 在C++的两个字符串中连接以逗号分隔的唯一值
- C++ 实现模板单例类时出现链接错误
- OOP 中的单成员指针
- 共享指针和具有自定义删除程序的唯一指针之间的语法差异背后的任何原因
- 从C 中的两个字符串中找到唯一的常见单词
- 当链表是列表中唯一的节点时,删除链表的头部.单联
- 使用QT C 为Sevaral表单创建常见的数据库连接
- 唯一表单算法和列表容器的唯一性有什么区别