首页 > 学院 > 开发设计 > 正文

String 、char* 的相互转换

2019-11-06 08:18:35
字体:
来源:转载
供稿:网友

Task


Implement the two functions which PRoceed to exchange between string and cstring. (1) std::string change1(const char* cstr);

Convert a string of the cstring type to one belonging to the string type. (2) void change2(std::string str, char* cstr);

Convert a string of the string type to one belonging to the cstring type.


main.cpp

#include <cstdio>#include <iostream>#include "exchange.h"std::string str;char cstr[100];int main() { std::cin >> str; change2(str, cstr); puts(cstr); scanf("%s", cstr); std::cout << change1(cstr) << std::endl; return 0;}

exchange.h*

#include <cstring>std::string change1(const char* cstr){ std::string str_; str_ = cstr; return str_;}void change2(std::string str, char* cstr){ strcpy(cstr,str.c_str());}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表