比较C ++中的字符串 [英] Compare string in C++

查看:99
本文介绍了比较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''' )){* / b $ b cout<<"'FOUND letter E at"<<<" "<< count<< endl;

}


count ++;

}

返回1; < br $>
}

输出:

FOUND字母E at 0

FOUND字母E at 1

FOUND字母E在2

FOUND字母E在3

FOUND字母E在4


有些问题与 ; if(STR [count] =(''E'')"


我希望输出为:

FOUND letter at 1


请给我一些提示。

谢谢,

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

推荐答案

tv****@hotmail.com 写道:
#include< iostream>
#include< string>
int main(void){

使用命名空间std;
字符串STR =(" TEST");
const std :: string :: size_type STR_SIZE = STR.size();

int count = 0;
while(count< = STR_SIZE){
if(STR [ count] =(''E'')){
co ut<<<<" FOUND letter E at"<<<" "<< count<< endl;
}
数++;
}
返回1;
}
输出:
FOUND字母E在0
找到字母E在1
找到字母E在2
找到字母E在3
找到字母E在4

有些不对劲用if(STR [count] =(''E'')"

我希望输出为:
FOUND letter at 1

请给我一些提示。
谢谢,
#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,




=是赋值运算符。

==是测试平等运营商。


-

Scott McPhillips [VC ++ MVP]



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

--
Scott McPhillips [VC++ MVP]


tvn ... @ hotmail.com写道:
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)){


当心,=不是==。第一个分配,第二个分配。


if(STR [count] ==(''E''))

cout<< ;FOUND letter E at<<<" "<< count<< endl;
}

count ++;
}
返回1;
}
#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)下次格式化代码。

2)大写字母应保留给宏

3)main()应返回0,EXIT_SUCCESS或EXIT_FAILURE

Jonathan



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




Jonathan Mcdougall写道:

Jonathan Mcdougall wrote:
tvn。 .. @ hotmail.com写道:
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){
#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){




顺便说一句,这是无效的,你会读一个过去的最后一个

字符。制作


while(count< STR_SIZE)

Jonathan



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

while (count < STR_SIZE)
Jonathan


这篇关于比较C ++中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆