Copare two strings

J

J4CK4L

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!
 
E

Eric Jensen

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
 
J

J4CK4L

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!
 
E

Eric Jensen

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) {
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.
 
J

J4CK4L

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!
 
E

Eric Jensen

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
 
L

Luke Meyers

Eric said:
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) {
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
 
J

J4CK4L

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

Thomas J. Gritzan

Eric said:
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)

}
 
M

Markus Svilans

Eric said:
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.
 
R

red floyd

Markus said:
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.
 
M

Markus Svilans

red said:
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.
 
L

Larry I Smith

Markus said:
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
 
E

Eric Jensen

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
 
L

Larry I Smith

Eric said:
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
 
M

Markus Svilans

Eric said:
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,
 
L

Larry I Smith

Markus said:
Eric said:
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
 
L

Larry I Smith

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,169
Latest member
ArturoOlne
Top