By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,655 Members | 964 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,655 IT Pros & Developers. It's quick & easy.

Copare two strings

P: n/a
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!

Jul 2 '06 #1
Share this Question
Share on Google+
19 Replies


P: n/a

"J4CK4L" <fe**************@tiscali.itwrote in message
news:11**********************@b68g2000cwa.googlegr oups.com...
in the 'while' block I have to compare pch to a word in wordlist.txt.
Help me please!
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
Jul 2 '06 #2

P: n/a
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!

Jul 2 '06 #3

P: n/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.
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.
Jul 2 '06 #4

P: n/a
thx again,
You're right I must wrote c++ code.
Howewer I need to iterate in a file that is external to the class
(*.txt) because in the future it can be updated nope in a list
internal to te class eg. I think I should use 'fopen'.
thx again!

Jul 2 '06 #5

P: n/a
thx again,
You're right I must wrote c++ code.
Howewer I need to iterate in a file that is external to the class
(*.txt) because in the future it can be updated nope in a list
internal to te class eg. I think I should use 'fopen'.
thx again!
I would rather use fstream for reading the file.

fopen() is a part of the C standard library, and fstream (file-stream) is a
part of the C++ standard library.

#include <fstream>

std::fstream wordlist;
std::string aWordFromWordlist;

wordlist.open("wordlist.txt", std::fstream::in); // open file

while (wordlist.good()) {
wordlist >aWordFromWordlist;
std::cout << "Word from file: " << aWordFromWordlist << std::endl;
}

wordlist.close();

^^ the above code assumes that the words in the file is seperated by
whitespace or newline.

//eric
Jul 2 '06 #6

P: n/a
Eric Jensen wrote:
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.
I agree with this, but...
#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;
}
....this example is not standard C++. It's not even *valid* C++.

Luke

Jul 2 '06 #7

P: n/a
Eric THX THX THX!
I've finally made it,
thx for your patient and for your help.
Your'my angel my friend :).
Thx again!

Jul 2 '06 #8

P: n/a
Eric Jensen schrieb:
>thx again,
You're right I must wrote c++ code.
Howewer I need to iterate in a file that is external to the class
(*.txt) because in the future it can be updated nope in a list
internal to te class eg. I think I should use 'fopen'.
thx again!

I would rather use fstream for reading the file.

fopen() is a part of the C standard library, and fstream (file-stream) is a
part of the C++ standard library.
I don't like to be pedantic, but:
#include <fstream>
#include <iostream>
#include <string>

int main()
{
std::fstream wordlist;
std::string aWordFromWordlist;

wordlist.open("wordlist.txt", std::fstream::in); // open file

while (wordlist.good()) {
wordlist >aWordFromWordlist;
while (wordlist >aWordFromWordlist) {
std::cout << "Word from file: " << aWordFromWordlist << std::endl;
}

wordlist.close();
// not necessary (destructor closes the file)

}
^^ the above code assumes that the words in the file is seperated by
whitespace or newline.

//eric
--
Thomas
/* no comment */
Jul 3 '06 #9

P: n/a

Eric Jensen wrote:
"J4CK4L" <fe**************@tiscali.itwrote in message
news:11**********************@b68g2000cwa.googlegr oups.com...
in the 'while' block I have to compare pch to a word in wordlist.txt.
Help me please!

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
It is also useful to know that strcmp() does case-sensitive string
comparison. For case-insensitive string comparison, use stricmp().

HTH,
Markus.

Jul 4 '06 #10

P: n/a
Markus Svilans wrote:
>
It is also useful to know that strcmp() does case-sensitive string
comparison. For case-insensitive string comparison, use stricmp().

stricmp is not part of the Standard. It's a vendor specific extension.
Jul 4 '06 #11

P: n/a
red floyd wrote:
Markus Svilans wrote:

It is also useful to know that strcmp() does case-sensitive string
comparison. For case-insensitive string comparison, use stricmp().


stricmp is not part of the Standard. It's a vendor specific extension.

My Borland C++ Builder 6 documentation indicates that stricmp() is
supported by the Win32, ANSI C, and ANSI C++ standard libraries. From
my brief Google searching, it also appears that stricmp() is supported
in gcc as well as a large number of other C++ compilers.

There is also the strcmpi() function, which appears to be identical to
stricmp() and at least as widely supported.

Markus.

Jul 4 '06 #12

P: n/a
Markus Svilans wrote:
red floyd wrote:
>Markus Svilans wrote:
>>It is also useful to know that strcmp() does case-sensitive string
comparison. For case-insensitive string comparison, use stricmp().

stricmp is not part of the Standard. It's a vendor specific extension.


My Borland C++ Builder 6 documentation indicates that stricmp() is
supported by the Win32, ANSI C, and ANSI C++ standard libraries. From
my brief Google searching, it also appears that stricmp() is supported
in gcc as well as a large number of other C++ compilers.

There is also the strcmpi() function, which appears to be identical to
stricmp() and at least as widely supported.

Markus.
Hmm, then the Borland doc is out of date.

stricmp() (real name _stricmp() in MSVC) is non-standard.
MSDN-Online verifies this:

Name: Header: Compatability:
_stricmp(), <string.h>, Win 98, Win Me, Win NT, Win 2000, Win XP

With the MSVC compiler, 'stricmp' is a define for '_stricmp'
that is (was?) only available when compiler extensions are enabled.

The same goes for strdup(), and many other non-std functions.

It's not avail in my Windows, Unix, or Linux (gcc v3.3.5) libs.

Regards,
Larry
Jul 4 '06 #13

P: n/a

Larry I Smith wrote:

[snip]
With the MSVC compiler, 'stricmp' is a define for '_stricmp'
that is (was?) only available when compiler extensions are enabled.

The same goes for strdup(), and many other non-std functions.

It's not avail in my Windows, Unix, or Linux (gcc v3.3.5) libs.
My updated msdn 2005 states the following when searching for stricmp():
"These POSIX functions are deprecated beginning in Visual C++ 2005. Use the
ISO C++ conformant _stricmp, _wcsicmp, _mbsicmp, _stricmp_l, _wcsicmp_l,
_mbsicmp_l instead."

Anyway stricmp() / _stricmp() is evil and may cause unexpected behavoir.

//eric
Jul 4 '06 #14

P: n/a
Eric Jensen wrote:
Larry I Smith wrote:

[snip]
>With the MSVC compiler, 'stricmp' is a define for '_stricmp'
that is (was?) only available when compiler extensions are enabled.

The same goes for strdup(), and many other non-std functions.

It's not avail in my Windows, Unix, or Linux (gcc v3.3.5) libs.

My updated msdn 2005 states the following when searching for stricmp():
"These POSIX functions are deprecated beginning in Visual C++ 2005. Use the
ISO C++ conformant _stricmp, _wcsicmp, _mbsicmp, _stricmp_l, _wcsicmp_l,
_mbsicmp_l instead."

Anyway stricmp() / _stricmp() is evil and may cause unexpected behavoir.

//eric

MSDN Online does not mention 'POSIX' or 'ISO C++' for any of the
functions you mention. They're not in the std libs (C or C++)
on unix or linux. By definition, functions starting with an
underscore are non-std.

Regards,
Larry
Jul 5 '06 #15

P: n/a

Eric Jensen wrote:
Larry I Smith wrote:

[snip]
With the MSVC compiler, 'stricmp' is a define for '_stricmp'
that is (was?) only available when compiler extensions are enabled.

The same goes for strdup(), and many other non-std functions.

It's not avail in my Windows, Unix, or Linux (gcc v3.3.5) libs.

My updated msdn 2005 states the following when searching for stricmp():
"These POSIX functions are deprecated beginning in Visual C++ 2005. Use the
ISO C++ conformant _stricmp, _wcsicmp, _mbsicmp, _stricmp_l, _wcsicmp_l,
_mbsicmp_l instead."

Anyway stricmp() / _stricmp() is evil and may cause unexpected behavoir.

//eric
Is there a standard-compliant alternative for case-insensitive string
matching?

Markus,

Jul 5 '06 #16

P: n/a

"Larry I Smith" <la***********@verizon.netwrote in message
news:v2Fqg.4590$543.1734@trnddc04...
MSDN Online does not mention 'POSIX' or 'ISO C++' for any of the
functions you mention. They're not in the std libs (C or C++)
on unix or linux. By definition, functions starting with an
underscore are non-std.
http://msdn2.microsoft.com/en-us/library/ms235365.aspx

^^ The qoute.

//eric
Jul 5 '06 #17

P: n/a
Eric Jensen wrote:
"Larry I Smith" <la***********@verizon.netwrote in message
news:v2Fqg.4590$543.1734@trnddc04...
>MSDN Online does not mention 'POSIX' or 'ISO C++' for any of the
functions you mention. They're not in the std libs (C or C++)
on unix or linux. By definition, functions starting with an
underscore are non-std.

http://msdn2.microsoft.com/en-us/library/ms235365.aspx

^^ The qoute.

//eric

One more time....

These functions are NOT POSIX or ISO C++ compliant.

Here's another MSDN ref that will probably wrap in your NG Reader:

http://msdn.microsoft.com/library/de..._._mbsicmp.asp

So MSDN contradicts itself (no surprise there).
No matter, those functions are NOT Posix or ISO C++.

Regards,
Larry
Jul 5 '06 #18

P: n/a
Markus Svilans wrote:
Eric Jensen wrote:
>Larry I Smith wrote:

[snip]
>>With the MSVC compiler, 'stricmp' is a define for '_stricmp'
that is (was?) only available when compiler extensions are enabled.

The same goes for strdup(), and many other non-std functions.

It's not avail in my Windows, Unix, or Linux (gcc v3.3.5) libs.
My updated msdn 2005 states the following when searching for stricmp():
"These POSIX functions are deprecated beginning in Visual C++ 2005. Use the
ISO C++ conformant _stricmp, _wcsicmp, _mbsicmp, _stricmp_l, _wcsicmp_l,
_mbsicmp_l instead."

Anyway stricmp() / _stricmp() is evil and may cause unexpected behavoir.

//eric

Is there a standard-compliant alternative for case-insensitive string
matching?

Markus,
No.

Larry
Jul 5 '06 #19

P: n/a
Larry I Smith wrote:
Eric Jensen wrote:
>"Larry I Smith" <la***********@verizon.netwrote in message
news:v2Fqg.4590$543.1734@trnddc04...
>>MSDN Online does not mention 'POSIX' or 'ISO C++' for any of the
functions you mention. They're not in the std libs (C or C++)
on unix or linux. By definition, functions starting with an
underscore are non-std.
http://msdn2.microsoft.com/en-us/library/ms235365.aspx

^^ The qoute.
There is only one possible valid meaning for the word "conformant"
in the above link:

All non-Standard functions (extensions supplied by the
compiler or system libs) have names beginning with
an underscore. So, since these are non-standard functions
their names begin with an underscore - confoming to the
standard.

These functions have never been in the Posix standard either.

Regards,
Larry
Jul 5 '06 #20

This discussion thread is closed

Replies have been disabled for this discussion.