知识库

C++语言开发示例:16. 创建字符串

2025-02-01 20:31:05 李腾 1 次阅读

strgback.cpp

#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 << endl;
    delete[] ps;
    ps = buildstr('+', 20);
    cout << ps << "-DONE-" << ps << endl;
    delete[] ps;
}

char *buildstr(char c, int n)
{
    char *pstr = new char[n + 1];
    pstr[n] = '\0';
    while (n-- > 0)
        pstr[n] = c;
    return pstr;
}
转载请注明转自www.hylab.cn,原文地址:C++语言开发示例:16. 创建字符串

评论 (0)

登录后发表评论

暂无评论,快来发表第一条评论吧!