gets输入字符串,printf输出字符串
- c中无string基本类型
- c++程序操作
#include <iostream>
#include <cstring>//用string 必须引用
#include <stdio.h>//用printf 必须引用
using namespace std;
char ch[20];
gets(ch);//输入,遇到换行符停止输入
string str=ch;//char数组转string型
printf("%s\n",str.data());//str.data()是char*型
c中输出char型数组
char name[12]="zhixun";
printf("%s\n",name);//输出zhixun
char型比较大小
char可以直接用> 、<、 ==这些比较大小
string替换特定字符(replace方法)
c++中string的替换,貌似没找到直接替换字符串的,感觉没Java方便
#include <iostream>
#include <cstring>
#include <stdio.h>
#include <algorithm>//必须引用
using namespace std;
replace(str.begin(),str.end(),'I','O');//用O替换I,但是这里只能是替换字符,不能是字符串
str.replace(str.find("In"),1,"Out");//str中出现In的第一个位置,把I替换成Out
string与数字的拼接
Java中直接用“+”即可,但是c++中不行
int temp=1;
string str="Sample Output";//想将str与temp拼接
temp++;
printf("%s%d\n",str.data(),temp);//输出的时候就直接拼接了,暂时只知道这个方法