以C++运行:C字符数组 类型转换

#include<iostream>
using namespace std;
int main(){
	char b[10]="3.14";
	double a=atof(b);   //字符串数组char转double 
	//cout<<a+0.1<<endl;
	
	char b1[10]="114514";
	int c=atoi(b1);   //字符串数组char转int 
	//cout<<c-4<<endl;
	
	char b2[20]="12345678912345";
	long long d=atoll(b2);   //字符串数组char转long long
	//cout<<d+1<<endl;
	
	char b3[20]="114514";
	char *p;
	int e=strtoll(b3, &p, 10);
	//cout<<e;
	return 0;
}

以C++运行:C字符数组 基础操作

#include<iostream>
#include<cstring>
using namespace std;
int main(){
	char a[10]="abc";
	char b[10]="123";
	
	strcpy(b, a);     //复制
	//cout<<b<<endl;

	strcat(b, a);     //拼接
	//cout<<b<<endl;
	
	//cout<<strcmp(a, b);     //比较(a==b返回0  a>b返回1  a<b返回-1)
	
	strlen(a);     //获取长度 
	return 0
}

C++字符数组 基础操作

#include<iostream>
#include<cstring>
using namespace std;
int main(){
	string s;
	cin>>s;     //不能获取空格
	getline(cin, s);     //能获取带空格的字符串 
	
	
	s.length();     //获取长度
	cout<<s.substr(3, 4);     //截取子串  参数1:字串起始位置,参数2:字串的长度 
	cout<<s.find("12");     //查找字串,输出字串的起始位置 
	s.clear();     // 清空字符串
	
	return 0; 
}

5 条评论

  • 1