Compression Code
Mar 3, 2009 at 7:31pm UTC
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?
Mar 4, 2009 at 4:15am UTC
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 !
Mar 4, 2009 at 5:38am UTC
@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.
Mar 4, 2009 at 5:39am UTC
Sorry man. I'll remember it from next time.
Mar 4, 2009 at 7:11am UTC
It's their problem, anyway...if they copy someone else's, they are the ones that suffer. (They just don't know it, yet.)
Mar 4, 2009 at 6:04pm UTC
@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.