C++函数返回C风格的字符串

2021/7/1 14:51:16

本文主要是介绍C++函数返回C风格的字符串,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

函数返回C风格的字符串:
函数无法返回一个字符串,可以返回一个字符串的地址,这样做的效率更高。

#include <iostream>
char* buildstr(char c,int n);
int main()
{
	using namespace std;
	int times;
	char ch;

	cout << "Enter a character: ";
	cin >> ch;
	cout << "Enter an integer: ";
	cin >> times;
	char* ps = buildstr(ch,times);
	cout << ps << "DONE-" << ps << endl;
	return 0;
}
char* buildstr(char c, int n)
{
	char* pstr = new char[n+1];
	pstr[n] = '\0';
	while (n-->0)
	{
		pstr[n] = c;
	}
	return pstr;
}




这篇关于C++函数返回C风格的字符串的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程