123456789101112131415161718192021222324252627282930313233 |
- // strgback.cpp -- a function that returns a pointer to char
- #include <iostream>
- char * buildstr(char c, int n); // prototype
- 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; // free memory
- ps = buildstr('+', 20); // reuse pointer
- cout << ps << "-DONE-" << ps << endl;
- delete [] ps; // free memory
- // cin.get();
- // cin.get();
- return 0;
- }
- // builds string made of n c characters
- char * buildstr(char c, int n)
- {
- char * pstr = new char[n + 1];
- pstr[n] = '\0'; // terminate string
- while (n-- > 0)
- pstr[n] = c; // fill rest of string
- return pstr;
- }
|