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;
}

find_char algorithm is wrong, bec becouuse when the first time you pass argument it will return 0. Did you think about sorting char as int number then removing duplicates, thant is very simplee..;)

I'm not going to give any code but I will help you out with the logic.
1) Set a counter for letters removed to 0.
2) Using a for loop grab a char from the array.
3) Using another for loop start from the position of the char you grabbed go to the end checking for like letters.
4) If you find a duplicate set it to "" and add 1 to letters removed.
5) Continue looping through the loop from step one untill you reach the end of the array.
6) After the loop finishes return size - letters removed for the new size

Edited 5 Years Ago by NathanOliver: n/a

This article has been dead for over six months. Start a new discussion instead.