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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
#include <iostream>
#include <string>
using namespace std;
int startingSize = 0, removedSize = 0;
int* removedPtr = &removedSize;
string input;
char text[140];
void deleteRepeats(int startingSize, int* removedPtr, char text[]);
bool isInArray(char text[], int StartingSize, char target);
int main(){
cout << "Enter 140 characters or less: ";
getline(cin, input);
cout <<"\nYou entered: " <<endl <<input <<endl;
char* arrayPtr= text;
for (int i=0; i < input.length(); i++) {
*(arrayPtr + i) = input[i];
}
startingSize = input.length();
cout <<"Loaded Array: ";
for (int i=0; i <= startingSize; i++)
cout << text[i];
cout <<endl;
cout <<"Starting Size: " <<startingSize <<endl;
cout <<"\nDeleting Repeats: " <<endl;
deleteRepeats(startingSize, removedPtr, text);
return 0;
}
//Move non-duplicates to temporary array, then copy to a dynamic array of exact size.
void deleteRepeats(int startingSize, int* removedPtr, char text[]) {
char* temp = new char [startingSize];
for (char* ptr = &text[0]; ptr < ptr + startingSize; ptr++) {
if (isInArray(text, startingSize, *ptr) == false) {
*temp = *ptr;
temp++;
}
else *removedPtr++;
}
char* trimmedArray = new char [startingSize - *removedPtr];
for (char* ptr = &temp[0]; ptr < ptr + (startingSize - *removedPtr); ptr++) {
*(trimmedArray++) = *(temp++);
}
}
bool isInArray(char arr[], int StartingSize, char target) {
//cout <<"\n IsInArray called. \n";
char* ptr= &arr[0];
while ((ptr < ptr+startingSize) && (*ptr != '\n')) {
if (*ptr == target) return 1;
ptr++;
}
return 0;
}
|