Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Help with strncpy - crypt code

szylviaszylvia Posts: 5Member
Hi everybody!
This is my first post in this forum : )

I am newbie but with a huge interest to learn Borland C++ ( 5.5.1) ( or perhaps I should choose C++Builder XE ?) (I always need just small pieces of code like this one here, with no interface or any other stuff, that

Comments

  • pseudocoderpseudocoder Posts: 682Member ✭✭✭
    I took a quick stab at an encrypt and decrypt variation; not sure how many bugs exist or whether or not it will be of any use... added the space for entering phrases.

    [code]
    #include
    #include
    #include

    static char* letter = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_-+=[{]};:|,<.>?~` ";
    static char* codes[] = {"duL","1TQ","YTp","IoU","1oM","goJ","ooQ","oof","got","kdo","toh","ko5","oJo","nCo","6Bo","BXo","loq","0oY","oG3","4x0","wF4","wkp","i6e","Z8Y","PKx","KZg","b5p","72Y","IX5","x6q","0IK","AiM","jCK","CBH","nGO","kvp","516","xsN","jKF","12S","iFc","7Sc","Yz2","n2b","xdJ","kGR","2Po","DlY","UYA","VR5","8RJ","6pB","FHi","LG6","ZNl","TWy","hqx","D1X","RNV","iY3","9Fq","JsW","p1U","uO0","i8j","CYX","WPl","OKu","ZeF","Nca","nz9","YM8","ijk","Rfx","KnD","2Um","jqT","CVx","dsE","YRz","5OD","L9Q","jaA","5RG","sv1","dlA","DRB","cZW","WYY","nS7", "XZ0" };


    char *encode(const char *src) {

    char *tmp = NULL;

    if((tmp = malloc((strlen(src) * 3) + 1)) != NULL) {
    strcpy(tmp, "");
    while(*src) {
    strcat(tmp, codes[strchr(letter, *src) - letter]);
    ++src;
    }
    }

    return tmp;
    }

    char *decode(const char *src) {

    int idx = 0, i, j=0;
    char *tmp = NULL,
    buf[4];

    if((tmp = malloc((strlen(src) / 3) + 2)) != NULL) {

    while(idx < strlen(src)) {
    memset(buf, 0, sizeof buf);
    strncpy(buf, (src + idx), 3);
    for(i=0; i < sizeof(codes)/sizeof(codes[0]); ++i) {
    if(strcmp(buf, codes[i]) == 0) {
    *(tmp + j++) = letter[i];
    break;
    }
    }

    idx += 3;
    }
    }

    *(tmp + j) = '';
    return tmp;
    }

    int main(void) {

    char *test = "a simple string to try out",
    *encoded = encode(test),
    *decoded = decode(encoded);

    if(encoded && decoded) {
    puts(encoded);
    puts(decoded);
    free(encoded);
    free(decoded);
    }

    return 0;
    }[/code]
  • szylviaszylvia Posts: 5Member

    Hi pseudocoder!
    Thank you for the code and fast reply ! I appreciate that and I can see that you change a lot my code..sorry for the mess, I am still learning coding..
    Unfortunately, when I compile it I get this message below , and both of them refers to your line :
  • szylviaszylvia Posts: 5Member

    Hi pseudocoder!
    Thank you for the code and fast reply ! I appreciate that and I can see that you change a lot my code..sorry for the mess, I am still learning coding..
    Unfortunately, when I compile it I get this message below , and both of them refers to your line :
  • pseudocoderpseudocoder Posts: 682Member ✭✭✭
    hello; glad to help out if I can. there are folks here who know their stuff, but I'm more of a hobbyist. :)

    try casting malloc; it's been a while since I've done any programming on a regular basis and can't recall all of the particulars. I don't use Borland, and didn't run into that problem.

    [code]
    if((tmp = (char *)malloc((strlen(src) * 3) + 1)) != NULL) {
    ...
    }[/code]

    welcome to the board.
  • szylviaszylvia Posts: 5Member
    Hello

    THANKS A LOT for the quick response..I really can see a light in the end of the tunnel .. : )

    I compiled that here and it worked, however, when I put it into my main code and just rename your function from
  • szylviaszylvia Posts: 5Member
    Hi there!
    I fixed it here now, thanks again!!
    I will try it out more and let you (and the others )know the result later on
    I look forward to your contact
    Thanks again!
    S
Sign In or Register to comment.