-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetline.cpp
More file actions
41 lines (35 loc) · 746 Bytes
/
getline.cpp
File metadata and controls
41 lines (35 loc) · 746 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
using namespace std;
void myswap(int *, int *);
char * getline();
main() {
char * a = getline();
for (char * i = a ; *i != '\0'; i++) {
cout << *i;
}
cout << endl;
delete [] a;
a = 0;
}
char *getline() {
int size = 4;
char * a = new char[size];
int i = 0;
char c;
while (cin.get(c) && c != '\n') {
if (i == size) {
//Reallocate array
size *= 2;
char * new_array = new char[size];
for (int j = 0; j < size / 2; j++) {
new_array[j] = a[j];
}
delete [] a;
a = new_array;
}
a[i] = c;
i++;
}
a[i] = '\0';
return a;
}