KIMBERLY C
2009-02-13 00:59:29 UTC
Write a function called delete_repeats that has a partially filled array of characters as a formal parameter and that deletes all repeated letters from the array. Since a partially filled array requires two arguments, the function will actually have two formal parameters: an array parameter and a formal parameter of type int that gives the number of array positions used. When a letter is deleted the remaining letters are moved forward to fill in the gap. This will create empty positions at the end of the array, so that less of the array is used. Since the formal parameter is a partially filled array, a second formal parameter of type int will tell how many positions are filled. This second formal parameter will be a call-by-reference parameter and will be changed to show how much of the array is used after the repeated letters are deleted.
This program will will ask the user to type in a sentence
and then will delete all repeated characters of the sentence.
The program will then output the new sentence with all repeated
letters deleted
p.s. it only displays 4 letters. why...
//code
#include
#include
using namespace std;
void fill_array(char a[], int size, int& number_used);
//Array a[] is filled with data from the keyboard
void delete_repeats(char a[], int& number_used);
//Function will remove all repeated characters and move the rest of the characters
//foward to fill in the gap.
void output(char a[], int& number_used);
//Outputs the contents of the array and outs the new size of the array
int main()
{
char array[100];
int number_used;
cout << "This program will will ask the user to type in a sentence\n"
<< "and then will delete all repeated characters of the sentence.\n"
<< "The program will then output the new sentence with all repeated\n"
<< "letters deleted\n";
fill_array(array, 100, number_used);
delete_repeats(array, number_used);
output(array, number_used);
system("PAUSE");
return 0;
}
//uses iostream
void fill_array(char a[], int size, int& number_used)
{
char c;
int index = 0;
cout << "Please type in a sentence and then press enter.\n";
cin.get(c);
while (c != '\n' && index < size)
{
index++;
a[index] = c;
cin.get(c);
}
number_used = index;
}
//uses iostream
void delete_repeats(char a[], int& number_used)
{
for (int i = 0; i < number_used; i++)
{
for (int j = i + 1; j < number_used; j++)
{
if (a[i] == a[j])
{
for (int k = j; k < number_used; k++)
a[k] = a[k + 1];
}
}
}
number_used = sizeof a;
}
//uses iostream
void output(char a[], int& number_used)
{
cout << "The new sentence without the repeated letters is:\n";
for (int i = 0; i < number_used; i++)
{
cout << a[i];
}
cout << "\nThe size of the new array is "
<< number_used
<< endl;
}
//output
Please type in a sentence and then press enter.
hello
The new sentence without the repeated letters is:
xhel
The size of the new array is 4
Press any key to continue . . .