c++中字符数组与字符串的转换
2013-12-02 15:14
267 查看
来源于互联网
1:字符数组换为字符串
见代码
[cpp] view
plaincopyprint?
#include
#include
using namespace std;
int
main(){
char
a[10]="aaaabbbba";
string
s(&a[0],&a[strlen(a)]);
cout<<s<<endl;
system("pause");
}
2:把字符串转换为字符数组
见代码:
[cpp] view
plaincopyprint?
#include
#include
using namespace std;
int
main(){
string s="aaaavvva";
char
a[10];
strncpy(a,s.c_str(),s.length());
for(int i=0;i<10;i++)
cout<<a[i]<<" ";
cout<<endl;
system("pause");
}
[b]3:把字符串转换为数字[/b]
[cpp] view
plaincopyprint?
#include
using namespace std;
int main()
{
char a='1';
int
x;
x=atoi(a);
cout<<x<<endl;
getchar();
return 0;
}
1:字符数组换为字符串
见代码
[cpp] view
plaincopyprint?
#include
#include
using namespace std;
int
main(){
char
a[10]="aaaabbbba";
string
s(&a[0],&a[strlen(a)]);
cout<<s<<endl;
system("pause");
}
#include #include using namespace std; int main(){ char a[10]="aaaabbbba"; string s(&a[0],&a[strlen(a)]); cout<<s<<endl; system("pause"); }
2:把字符串转换为字符数组
见代码:
[cpp] view
plaincopyprint?
#include
#include
using namespace std;
int
main(){
string s="aaaavvva";
char
a[10];
strncpy(a,s.c_str(),s.length());
for(int i=0;i<10;i++)
cout<<a[i]<<" ";
cout<<endl;
system("pause");
}
#include #include using namespace std; int main(){ string s="aaaavvva"; char a[10]; strncpy(a,s.c_str(),s.length()); for(int i=0;i<10;i++) cout<<a[i]<<" "; cout<<endl; system("pause"); }
[b]3:把字符串转换为数字[/b]
[cpp] view
plaincopyprint?
#include
using namespace std;
int main()
{
char a='1';
int
x;
x=atoi(a);
cout<<x<<endl;
getchar();
return 0;
}
相关文章推荐
- C++ 字符串,字符数组,数字之间转换
- C++ 字符串,字符数组,数字之间转换
- c++中字符数组与字符串的转换
- c++中字符数组与字符串的转换
- c++中字符数组与字符串的转换
- c++中字符数组与字符串的转换
- 九度oj+原始一个字符串左右交换字符最少步数变成含有2012字串+bfs及字符串和整型转换以及没有标记visit数组大小,只能用stl中的vecotr,实际上set更好。有count函数
- 字符串与字符数组的相互转换
- JAVA将 带有空格或特殊字符的字符串转换成int数组的方法
- C++ 中字符串操作--宽窄字符转换的实例详解
- *Delphi字符串与字符数组之间的转换(初始化的重要性)
- java中如何将字符数组转换成字符串,以逗号分割
- c++ primer plus 第五版 第七章 编程练习 第 8 题 (字符数组转成字符串,trim使用,判断空行)
- 关于C中字符数组,字符指针以及C++中string类型的两两转换及排序
- 字符数组相互转换成字符串
- 字符串和字符数组之间的转换
- C++指针和数组:字符和字符串、字符数组
- C/C++(字符串处理)字符指针、字符数组用法大总结
- 字节数组 整型 字符串 十六进制 字符 之间的转换
- C/C++——声明一个到字符的指针,包含10个整数的数组,到包含10个整数的数组的引用,到字符串的数组的指针,到字符的指针的指针...