Trouble using pointer arithmetic with character arrays

Write your question here.

My program below is meant to delete repeat characters in an array by copying unique characters to a buffer array and then to a dynamic array of the exact size, which it hands back to the main function (requirement of school project).

However, the buffer array temp doesn't seem to be loading correctly. Any insight as to why would be much appreciated.


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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// ********************************************************************
// This program implements a function to return a dynamic array
// that deletes repeated characters.
// ********************************************************************

#include <iostream>

using namespace std;

char *delete_repeats(char letters[], int size, int& numLetters);
bool isInArray(char arr[], int size, char target);


// ====================
//     main function
// ====================

int main()
{
  char a[140];
  char *noRepeats = NULL;

  int size = 0;
  int numLetters;
  cout << "ENTER A STRING OF CHARACTERS \n";
  cout << "(up to 140 characters)\n";
  a[size] = cin.get();
  while ((size < 140) && (a[size] != '\n'))
  {
    size++;
    a[size] = cin.get();
  }
  numLetters = size;
  cout << "\nYOU ENTERED " << size << " CHARACTERS \n";
  for (int i=0; i<size; i++) cout << a[i] ;
  cout << endl;

  cout << "\nDELETING REPEATS\n";
  noRepeats = delete_repeats(a, size, numLetters);

  for (int i=0; i<numLetters; i++)
  {
          cout << noRepeats[i];
  }
  cout << endl;
  delete [] noRepeats;

  return 0;
}




// This function returns true if the given char
// is in the char array, and false otherwise
bool isInArray(char arr[], int size, char target)
{
char* ptr = arr;
for (int i=0; i < size; i++) {
	if (target == *(ptr + i))
		return true;
	}
return false;
}




// This function scans through the input letters
// and copies them into a temporary buffer.
// The temporary buffer is then copied into a
// dynamic array that exactly holds the data.
char* delete_repeats(char letters[], int size, int& numLetters)
{
        char *temp = new char [size];        // Holds unique chars
        char *ptr = letters;
	numLetters = 0;
for (int i=0; i<size; i++) {
	if (!isInArray(letters, size, *(ptr + i))) {
		*++temp = *++ptr;
		numLetters++;
	}
}

/*cout <<endl <<*temp;
cout <<endl;
for (int i=0; i < size; i++) {
	cout <<temp[i];
}
cout <<endl;
*/

	char *trimmed = new char [numLetters];		// Unique chars with no empty space
for (int i=0; i < numLetters; i++){
	*(trimmed + i) = *(letters);
}
delete [] temp;
        return trimmed;
}
Last edited on
Do I understand it correctly that you only want the charaters that occur only once in a string.
For example when I input "aabbccddef" that I should expect only ef ?

One problem I see is in line 79
if (!isInArray(letters, size, *(ptr + i)))

Since e and f are in the array they get skipped as well.

An easier way would be to count all characters from a-z and if it occurs exactly once copy it.

Topic archived. No new replies allowed.