Copare两个字符串 [英] Copare two strings

查看:174
本文介绍了Copare两个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

i想在一个名为wordlist.txt的文件中比较另一个字符串

我有这段代码......


int扫描(字符搜索[])

{

char * pch;

pch = strtok(搜寻, " - ,。");

while(pch!= NULL)

{

printf(" Scanning now word \ "%s \" \ n",pch);

pch = strtok(NULL," - ,。");

}
}


在''while''块中我必须将pch与wordlist.txt中的单词进行比较。

帮帮我拜托!

Hi everyone,
i want to compare a string whith another in a file called wordlist.txt
I''ve this code...

int Scan(char Search[])
{
char *pch;
pch = strtok (Search," -,.");
while (pch != NULL)
{
printf ("Scanning now word \"%s\"\n",pch);
pch = strtok (NULL, " -,.");
}
}

in the ''while'' block I have to compare pch to a word in wordlist.txt.
Help me please!

推荐答案



" J4CK4L" < fe ************** @ tiscali.itwrote in message

news:11 **************** ****** @ b68g2000cwa.googlegr oups.com ......

"J4CK4L" <fe**************@tiscali.itwrote in message
news:11**********************@b68g2000cwa.googlegr oups.com...
'''''块中的
我必须将pch与wordlist中的单词进行比较.txt。

请帮帮我!
in the ''while'' block I have to compare pch to a word in wordlist.txt.
Help me please!



你的代码看起来像它的C.像strtok一样,C标准库有一个函数

叫做strcmp(string1,string2)。参数必须为NULL终止

字符串。


例如


char a [] =" test" ;

char b [] =" test";


if(strcmp(a,b)== 0){

//字符串是相同的

}否则{

//字符串不相同

}


// eric

Your code look like its C. Like strtok the C standard library has a function
called strcmp(string1, string2). The parameters must be NULL terminated
strings.

e.g.

char a[] = "test";
char b[] = "test";

if (strcmp(a, b) == 0) {
// strings are identical
} else {
// strings are not identical
}

//eric


THX ERIC !!

又一个问题?

你写的


char b [];

char a [];


我从while块接收char b但是char我需要在一个用于计算wordlist.txt中ani字符串的for块中的
并比较一下

char a。

帮帮我请。

Thx再次!

THX ERIC!!
yet another question?
you wrote

char b[];
char a[];

I receive char b fro the while block but the char a I need to have it
in a for block that count ani string in wordlist.txt and compare whit
char a.
Help me please.
Thx Again!


我收到了来自while块的char b但是我需要它的char a
I receive char b fro the while block but the char a I need to have it

在for block中计算wordlist.txt中的ani字符串并比较whit

char a。
in a for block that count ani string in wordlist.txt and compare whit
char a.



基本上你需要做的是将你的文本文件

(wordlist.txt)中的所有字符串加载到某个集合中。然后在你的

中对它们进行比较。


你的代码看起来像C,但这是一个C ++组,所以我假设你的

在C ++中工作,但正在输入C样式代码。


如果使用C ++,将更容易实现想要实现的目标

标准库。


#include< tchar.h>

#include< iostream>

#include < windows.h>

#include< sstream>

int _tmain(int argc,_TCHAR * argv [])

{

std :: stringstream搜索(" word1 word2 word3 word4 word5");

std :: string CurrentWord;

std :: string wordlist_txt [2];

wordlist_txt [0] =" word1";

wordlist_txt [1] =" word2";

int i ;

while(Search.good()){

搜索> CurrentWord;

std :: cout<< 现在测试: << CurrentWord<< std :: endl;

for(i = 0; i< 2; i ++){

if(CurrentWord == wordlist_txt [i]){

std :: cout<< ''这个词'' << CurrentWord<< ''等于wordlist_txt [" <<

i<< "]" << std :: endl;

}

}

}

system(暂停);

返回0;

}


最好你应该写一个包含wordlist.txt的类,然后

创建一个成员函数,用于检查单词是否在列表中。例如

MyWordList.Contains(" word");你可以在你的循环中打电话。

Basicly what you need to do is to load all the strings from you text file
(wordlist.txt) into some collection. And then compare them all inside your
while block.

You code looks like C, but this is a C++ group so i''ll assume that your
working in C++ but are typing C style code.

What you want to accomplish will be more easily reached if you use the C++
standard library.

#include <tchar.h>
#include <iostream>
#include <windows.h>
#include <sstream>
int _tmain(int argc, _TCHAR* argv[])
{
std::stringstream Search("word1 word2 word3 word4 word5");
std::string CurrentWord;
std::string wordlist_txt[2];
wordlist_txt[0] = "word1";
wordlist_txt[1] = "word2";
int i;
while (Search.good()) {
Search >CurrentWord;
std::cout << "Now testing: " << CurrentWord << std::endl;
for (i=0; i<2; i++) {
if (CurrentWord == wordlist_txt[i]) {
std::cout << "The word ''" << CurrentWord << "'' is equal to wordlist_txt[" <<
i << "]" << std::endl;
}
}
}
system("pause");
return 0;
}

Preferably you should write a class that contains the wordlist.txt, and then
create a member function that will check if the word is in list. e.g.
MyWordList.Contains("word"); you can call in your loop.


这篇关于Copare两个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆