473,320 Members | 2,004 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,320 software developers and data experts.

Compare string in C++

#include <iostream>
#include <string>
int main (void){

using namespace std;
string STR =("TEST");
const std::string::size_type STR_SIZE = STR.size();

int count =0;
while ( count <= STR_SIZE){
if (STR[count]= ('E')){
cout <<"FOUND letter E at"<<" "<< count<<endl;
}

count++;
}
return 1;
}
output:
FOUND letter E at 0
FOUND letter E at 1
FOUND letter E at 2
FOUND letter E at 3
FOUND letter E at 4

Some thing wrong with "if (STR[count]= ('E')"

I would like to have the output as:
FOUND letter at 1

Please give me some hints .
Thanks,

Nov 3 '05 #1
10 4210
tv****@hotmail.com wrote:
#include <iostream>
#include <string>
int main (void){

using namespace std;
string STR =("TEST");
const std::string::size_type STR_SIZE = STR.size();

int count =0;
while ( count <= STR_SIZE){
if (STR[count]= ('E')){
cout <<"FOUND letter E at"<<" "<< count<<endl;
}

count++;
}
return 1;
}
output:
FOUND letter E at 0
FOUND letter E at 1
FOUND letter E at 2
FOUND letter E at 3
FOUND letter E at 4

Some thing wrong with "if (STR[count]= ('E')"

I would like to have the output as:
FOUND letter at 1

Please give me some hints .
Thanks,


= is the assignment operator.
== is the test for equality operator.

--
Scott McPhillips [VC++ MVP]

Nov 3 '05 #2
tvn...@hotmail.com wrote:
#include <iostream>
#include <string>
int main (void){

using namespace std;
string STR =("TEST");
const std::string::size_type STR_SIZE = STR.size();

int count =0;
while ( count <= STR_SIZE){
if (STR[count]= ('E')){
Watch out, = is not ==. The first one assigns, the second one compares.

if (STR[count] == ('E'))
cout <<"FOUND letter E at"<<" "<< count<<endl;
}

count++;
}
return 1;
}


1) Format your code next time.
2) Uppercase names should be reserved to macros
3) main() should return either 0, EXIT_SUCCESS or EXIT_FAILURE
Jonathan

Nov 3 '05 #3

Jonathan Mcdougall wrote:
tvn...@hotmail.com wrote:
#include <iostream>
#include <string>
int main (void){

using namespace std;
string STR =("TEST");
const std::string::size_type STR_SIZE = STR.size();

int count =0;
while ( count <= STR_SIZE){


And by the way, this is invalid, you'll read one past the last
character. Make it

while (count < STR_SIZE)
Jonathan

Nov 3 '05 #4
Thanks everyone for the help !!!!

Nov 3 '05 #5

<tv****@hotmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
#include <iostream>
#include <string>
int main (void){

using namespace std;
string STR =("TEST");
const std::string::size_type STR_SIZE = STR.size();

int count =0;
Should be:

std::string::size_type count = 0;
while ( count <= STR_SIZE){
STR[size] is out of bounds. Write:

while(count < STR_SIZE)

if (STR[count]= ('E')){
This *assigns*, does not compare. The equality
operator is ==
cout <<"FOUND letter E at"<<" "<< count<<endl;
}

count++;
}
return 1;
Only portable return values for main():

EXIT_SUCCESS /* defined by <cstdlib> */
EXIT_FAILURE /* defined by <cstdlib> */
0 (or any integer expression which evaluates to zero).
}
output:
FOUND letter E at 0
FOUND letter E at 1
FOUND letter E at 2
FOUND letter E at 3
FOUND letter E at 4
It found 'em there 'cause you put 'em there. :-)

Some thing wrong with "if (STR[count]= ('E')"
Yup. See above.

I would like to have the output as:
FOUND letter at 1

Please give me some hints .


Um, = != ==

Anyway, note that std::string has a 'find' function:

#include <iostream>
#include <string>

int main()
{
std::string s("TEST");
const char to_find('E');
const std::string::size_type pos(s.find(to_find));

if(pos != std::string::npos)
std::cout << "Letter '" << to_find << "' found at"
" position " << pos << '\n';
return 0;
}

-Mike
Nov 3 '05 #6
On 2005-11-03, tv****@hotmail.com <tv****@hotmail.com> wrote:
#include <iostream>
#include <string>
int main (void){

using namespace std;
string STR =("TEST");


Others have helped correct this program.

Here's what you should REALLY do. ;-)

for (string::size_type i = STR.find_first_of('E', 0)
; i != string::npos; i = STR.find_first_of('E', i+1))
{
cout << "FOUND letter E at " << i << endl;
}
cout << endl;
return 0;
}

In other words, you don't need to invent searching strings; the
library provides it.

--
Neil Cerutti
Nov 3 '05 #7
Jonathan Mcdougall wrote:
1) Format your code next time.
EMAIL systems are notoriously lousy for maintaining tabs.
2) Uppercase names should be reserved to macros
BULLSHIT. Mostly macros have no business in C++ at all.

3) main() should return either 0, EXIT_SUCCESS or EXIT_FAILURE

Perhaps, but not necessarily wrong. The return from main is
entirely implementation specific EVEN when restricted tot he
above values.
Nov 3 '05 #8
Mike Wahler wrote:
Only portable return values for main():

EXIT_SUCCESS /* defined by <cstdlib> */
EXIT_FAILURE /* defined by <cstdlib> */
0 (or any integer expression which evaluates to zero).

Even those values aren't portable. The return from main is
wholly implementation dependent. Further, there's no need
for the word "integer" in the above. The return will involve
an implicit conversion to int.
Nov 3 '05 #9
> > 2) Uppercase names should be reserved to macros
BULLSHIT. Mostly macros have no business in C++ at all.


Give the bloke a break! He's quite right in this regard (and his point
re returning from main is valid too). You might not _like_ macros, but
your "mostly" ain't worth much. Fact is macros are still essential, as
the only polymorphic mechanism allowing actual source code substitution
and generation (including identifier concatenation), and the only
mechanism effectively parsing code at the point of call (as far as
__FILE__ and __LINE__ are concerned). Traditionally, upper case names
_are_ used for macros, and for that reason should generally be avoided
for other uses (because the preprocessor lacks scoping features),
although some people like to use them for enumerated values, constants,
and/or template arguments.

Tony

Nov 3 '05 #10
Ron Natalie wrote:
Mike Wahler wrote:
Only portable return values for main():

EXIT_SUCCESS /* defined by <cstdlib> */
EXIT_FAILURE /* defined by <cstdlib> */
0 (or any integer expression which evaluates to zero).

Even those values aren't portable. The return from main is
wholly implementation dependent.


0 and EXIT_SUCCESS are required to indicate to the host environment
that execution succeeded, and EXIT_FAILURE is required to indicate
that execution failed. The actual value returned to the host
environment might be implementation-dependent, but that doesn't
matter, as long as the implementation draws the right conclusion
from that value. These values are "portable", by my understanding
of the word.

Nov 3 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Matt | last post by:
if (123 > 33) will return true and if ("123" > 33) will return true So my question is, if the above behaviors are the same?? If string is a number, and compare with another number, it will...
2
by: kw_uh97 | last post by:
Hello everone, I am a newbie and I have a recordset that returns the values XXXXMain XXXX1 XXXX2 XXXX3 XXXX4 XXXX5 XXXX6 YYYY1
10
by: lovecreatesbea... | last post by:
Is it correct and safe to compare a string object with "", a pair of quotation marks quoted empty string?If the string object: s = ""; does s contain a single '\'? Is it better to use...
1
by: swapnilladhe | last post by:
Hi, I have a array of string and a variable containing string and i code like as; if(String.compare(DFArray,checkBox.name)==0) { alert("R U HERE?"); f=false; ...
3
by: johnhelen | last post by:
Hello I have string of time like this 2007/09/19 18:55:14 Also i have a timestamp field in a postgres database with timezone like this 2004-10-19 10:23:54+02
7
pureenhanoi
by: pureenhanoi | last post by:
Hi bro! Can anyone tell me, how to compare two strings like Operating System compare file names. Example: if i have two string "T*.DOC" and "TIET1.DOC". The comparision operator must return TRUE...
7
by: sigava77 via AccessMonster.com | last post by:
Hi, I have a table with a string field that can store numeric field. How do I make a query to compare this field with a numeric value. Example: Select * from table1 where field1="condition" and...
4
by: ndoe | last post by:
how to compare string in file i mean compare that string content digit or alphabet,and 1 question more can we count that line if first line variable = 1,if read second line variable = 2 and so on...
6
by: webmaniac | last post by:
Hi, does anyone know, How to compare characters in 2 string & return How many characters match in both String. Like the First string is: "Hello World" And second is "This is a Word" So the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.