k33theod Δημοσ. 17 Δεκεμβρίου 2017 Δημοσ. 17 Δεκεμβρίου 2017 Δες και μια πιο ετσι προσεγγση int decode(const char* input, char* output, int szOutput); int encode(const char* input, char* output, int szOutput); void print(const char* s); void fillRandomRepeat(char* input, int len, const char* palet); int main(int, char**) { char buf[512] = { 0 }; char buf1[512] = { 0 }; char *it; int ret,i; fillRandomRepeat(buf, 511, "abcdefg"); printf("random data\n"); print(buf); ret = encode(buf, buf1, 511); printf("encoded data\n"); print(buf1); ret = decode(buf1, buf, 511); printf("decoded data\n"); print(buf); return 0; } int decode(const char* input, char* output, int szOutput) { int len = 0; const char* it = input; int charCount; for (; { charCount = atoi(it); if (!charCount) charCount = 1; //default; for (; !isalpha(*it) && *it; ++it); len += charCount + 1; if (szOutput < len && output) return -1; if (output) for (; charCount > 0; charCount--) *output++ = *it; if (*it++ == NULL) break; } return len; } int encode(const char* input, char* output, int szOutput) { const char* it = input; int len = 0; char buf[128]; int counter = 1; char currentChar = *it; if (strlen(input) < 2) return -1; do { it++; if (currentChar != *it) { if (counter > 1) { itoa(counter, buf, 10); len += strlen(buf); if (len > szOutput) return -1; strcpy(output, buf); output += strlen(buf); } if (len++ > szOutput) return -1; counter = 0; *output++ = currentChar; currentChar = *it; } counter++; } while (*it != NULL); return len; } void print(const char* s) { printf("======= data begin ==========\n%s\n========= data end ============\n", s); } void fillRandomRepeat(char* input, int len,const char* palet) { int i, paletSize; char r = *palet; paletSize = strlen(palet); for (i = 0; i < len; i++) { if (rand() % 100 < 30) r = palet[rand() % paletSize]; input[i] = r; } } Σταμάτα να στέλνεις virus
Προτεινόμενες αναρτήσεις
Δημιουργήστε ένα λογαριασμό ή συνδεθείτε για να σχολιάσετε
Πρέπει να είστε μέλος για να αφήσετε σχόλιο
Δημιουργία λογαριασμού
Εγγραφείτε με νέο λογαριασμό στην κοινότητα μας. Είναι πανεύκολο!
Δημιουργία νέου λογαριασμούΣύνδεση
Έχετε ήδη λογαριασμό; Συνδεθείτε εδώ.
Συνδεθείτε τώρα