Character Array printing "P<[random_character]"

I'm writing an algorithm that finds the first instance of each letter present in a string and then outputs each present letter in an alphabetical sequence.
For example: given the string "aaaaaaabbbbbbbbb" it would output "ab", and given the string "ddcaaaaahhhttt" it would output "acdht".

It seems to be having trouble giving a correct output; the print loop at the bottom giving "P<[random_character]". I assume it's a problem with how I'm assigning my values, but perhaps not? What am I doing wrong?

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
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

void catchFirsts(const string &inputString)
{
     char *FinalString = new char[52];
     int i = 0;
     
     for(char j = 'a'; j <= 'Z'; j++)
     {              
              int found = inputString.find_first_of(j, 0);

              if(found >= 0){
                       FinalString[i] = j;
                       i++;
              }
              if(found == -1){
                       break;
              }
              if(j == 'z'){j = 'A' - 1;}
     }
     for(int k = 0; FinalString[k] != '\0'; k++){
             cout << FinalString[k];
     }
     cout << endl;
     delete FinalString;
}        


int main(int argc, char *argv[])
{
    string test0 = "aaaaaaabbbbbb";
    string test1 = "aaaaaaabbbbbbccdd";
    string test2 = "abacdefkabfhxy";
    string test3 = "abacdefkabfhixy";
    string test4 = "abclmzbnopsw";
      
    catchFirsts(test0);
    
    system("PAUSE");
    return EXIT_SUCCESS;
}
Topic archived. No new replies allowed.