Compression Code

How can you make a program in C that does the following:
input string: My schoool.
compresed: My scho3l.

Program should check input string and display another string without duplicate values, how can you make this program?
If the whole purpose is to have a unique alphabets in the string then may you can use a map. Where storing alphabets as keys and count as values.
Use this code:
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
#include<iostream>
#include<map>
using namespace std;

int main() {

char arr[100];
cin.getline(arr,100);

 map <char, int> myMap;

for (int i=0;i<strlen(arr);i++) {

     if (myMap.count(arr[i]) == 0) {
         myMap.insert(pair<char, int>(arr[i], 1)) ;
     }
     else {
          if (arr[i] != ' ') {
             myMap[arr[i]]++;
        }
     }

}

char res[100];
map <char, int>::iterator it;

int index = 0;
for (int j=0;j<strlen(arr); j++) {
     if ( j == 0) {
         it = myMap.find(arr[j]);
          res[index] = (*it).first;
          index++;
     }
     else {
           if (arr[j] != arr[j-1]) {
            it = myMap.find(arr[j]);

          if ((*it).second > 1) {
           res[index] = (*it).first;
            index++;
            char ab[1];
            sprintf(ab,"%d",(*it).second);
            res[index] = ab[0];
            index++;
         }
         else {
             res[index] = (*it).first;
             index++;
         }
      }
    }
}

res[index] = NULL;
cout<<"RES:"<<res<<endl;

return 0;

 }


Hope this helps !
@kevin: Don't give out free code, it's better if you give them hints and tell them where to go...they learn better that way.
Sorry man. I'll remember it from next time.
It's their problem, anyway...if they copy someone else's, they are the ones that suffer. (They just don't know it, yet.)
@seymore15074

I couldn't agree with you more. A person who has to learn will learn no matter what and the person who does not have to learn will not learn no matter what.
We can't force anyone.

Peace
Topic archived. No new replies allowed.