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

Compare string in C++

P: n/a
#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
Share this Question
Share on Google+
10 Replies


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

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

P: n/a

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

P: n/a
Thanks everyone for the help !!!!

Nov 3 '05 #5

P: n/a

<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

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

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

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

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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.