Hello, I'm having problems with one of my codes that I need to finish for class.
I'm supposed to write a function that has a partially filled array of characters and it deletes all repeated letters from the array. The program requires input for the size of the array, and also characters. The output of the program should be the array size and the array display before I do anything, and then the after array size and array display after deleting repeated letters. My code thus far is:
Any help would be greatly appreciated! Thanks!
#include <iostream>
using namespace std;
void delete_repeats(char a[], int& size);
bool find_char (char target, char a[], int size);
int main()
{
char a[100];
int size = 10;
bool validation = true;
while(validation == true)
{
cout << "Please enter the a size of the array that is less than 100: \n";
cin >> size;
if(size > 100 || size < 0)
{
cout << "You have entered an invalid number.\n";
cout << "Please enter a number greater than 0\n";
cout << "and less than 100.\n";
}
else
break;
}
cout << "Please enter the characters you wish to input: \n";
cin >> a[0];
cout << "Before size = " << size << " array = " << endl;
for(int i = 0; i < size; i++)
{
cout << a[i];
}
cout << endl;
delete_repeats(a, size);
cout << "After size = " << size << " array = " << endl;
for(int i = 0; i < size; i++)
{
cout << a[i];
}
cout << endl;
system("pause");
return 0;
}
bool find_char (char target, char a[], int size)
{
for (int i = 0; i < size; i++)
{
if (a[i] == target)
return true;
}
return false;
}
void delete_repeats(char a[], int& positions_used)
{
int new_size = 0;
for(int i = 0; i < positions_used; i++)
{
if(!find_char(a[i], a, new_size))
{
a[new_size] = a[i];
new_size++;
}
}
positions_used = new_size;
}