473,395 Members | 2,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Copare two strings

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
19 4008

"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
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
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
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
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
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
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
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

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

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

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

"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Ravi | last post by:
Hi, I have about 200GB of data that I need to go through and extract the common first part of a line. Something like this. >>>a = "abcdefghijklmnopqrstuvwxyz" >>>b = "abcdefghijklmnopBHLHT"...
17
by: Gordon Airport | last post by:
Has anyone suggested introducing a mutable string type (yes, of course) and distinguishing them from standard strings by the quote type - single or double? As far as I know ' and " are currently...
16
by: Paul Prescod | last post by:
I skimmed the tutorial and something alarmed me. "Strings are a powerful data type in Prothon. Unlike many languages, they can be of unlimited size (constrained only by memory size) and can hold...
4
by: agent349 | last post by:
First off, I know arrays can't be compared directly (ie: if (arrary1 == array2)). However, I've been trying to compare two arrays using pointers with no success. Basically, I want to take three...
25
by: Rainmaker | last post by:
Hi, Can anyone tell me an efficient algorithm to sort an array of strings? Keep in mind that this array is HUGE and so the algorithm should me efficient enough to deal with it. Thanks
6
by: Broeisi | last post by:
Hello, I wrote the tiny progam below just to understand arrays and strings better. I have 2 questions about arrays and strings in C. 1. Why is it that when you want to assign a string to an...
14
by: manstey | last post by:
Hi, Is there a clever way to see if two strings of the same length vary by only one character, and what the character is in both strings. E.g. str1=yaqtil str2=yaqtel they differ at str1...
2
by: Potiuper | last post by:
Question: Is it possible to use a char pointer array ( char *<name> ) to read an array of strings from a file in C? Given: code is written in ANSI C; I know the exact nature of the strings to be...
95
by: hstagni | last post by:
Where can I find a library to created text-based windows applications? Im looking for a library that can make windows and buttons inside console.. Many old apps were make like this, i guess ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.