C++ 算是string和for loop的问题吧? 不清楚。。关于判定空格的。。

C++\u5982\u4f55\u5c06string\u6309\u7a7a\u683c\u5206\u5272\uff1f

#include
#include
#include

void main()
{
std::string o_str = "sadf sdfd asd asdf";
std::vector str_list; // \u5b58\u653e\u5206\u5272\u540e\u7684\u5b57\u7b26\u4e32
int comma_n = 0;
do
{
std::string tmp_s = "";
comma_n = o_str.find( " " );
if( -1 == comma_n )
{
tmp_s = o_str.substr( 0, o_str.length() );
str_list.push_back( tmp_s );
break;
}
tmp_s = o_str.substr( 0, comma_n );
o_str.erase( 0, comma_n+1 );
str_list.push_back( tmp_s );
}
while(true);
system( "pause" );
}

\u80af\u5b9a\u662fstr\u6700\u540e\u6709\u4e00\u4e2a\u7a7a\u683c\uff0c\u5373str\u91cc\u9762\u662f"aaa "\uff0c\u4f60\u68c0\u67e5\u4e00\u4e0b\u3002\u8fd9\u7a7a\u683c\u80af\u5b9a\u4e0d\u662f\u8fde\u63a5\u7684\u65f6\u5019\u81ea\u52a8\u52a0\u4e0a\u7684\u3002

\u8865\u5145\u89e3\u91ca\uff1a
\u4f60\u8fd8\u662f\u6ca1\u770b\u61c2\u662f\u600e\u4e48\u7684\uff1f
"111"\u540e\u9762\u6ca1\u6709\u7a7a\u683c\uff0c"111"+"aaa "="111aaa "\u8fd9\u4e0d\u5f88\u6b63\u5e38\u5417\uff0c\u4f60\u6ce8\u610f\u770baaa\u540e\u9762\u6709\u4e00\u4e2a\u7a7a\u683c\uff01

\u5c31\u50cf\u540e\u9762\u4e09\u4f4d\u8bf4\u7684\u90a3\u6837\uff0c\u4f60\u5bf9str\u505a\u4e00\u6b21trim\u5e94\u8be5\u5c31\u628a\u7a7a\u683c\u622a\u6389\u4e86\u3002

#include<stdio.h>
#include<string.h>

struct {
char *v;
char *t;
} dict[]={
"he","他",
"is","是",
"a","一个",
"good","好",
"man","人",
0,0
};

char *trans(char *s){
int k;
for(k=0;dict[k].v;k++ )
if(!stricmp(s,dict[k].v))
return dict[k].t;
return s;
}
main(){
char t[]="He is a good man.";
char word[20]="",*p, *w=word, c;
for(p=t; c=*p; p++){
if(c>='A'&&c<='Z') c+=0x20;
if(c>='a'&&c<='z')
*w++=c;
else{
*w=0;
printf("%s",trans(word));
w=word;
}
}
}

想法把string转化为char型数组,然后用int strspn(char*,cmp),返回第几个位是空格,其中cmp可以这样定义 char *cmp=“ ”,是双引号,你没有看错。

#include <iostream>
#include <string>

using namespace std;

int main()
{
string text(""),result("");
while(text != "endl"){
cin>>text;
//result += 翻译(text);
};
cout<<result;
return 1;
}
endl可以改成其他的特殊字符或字符串来结束输入

扩展阅读:c++编程 ... c++ string拼接 ... c++ string指针 ... c++ string类型 ... string在c++中的用法 ... string方法大全c++ ... c++cstring头文件 ... c++菜鸟教程 ... c++ string 长度 ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网