Beginner C/C++

Moderators: None (Apply to moderate this forum)
Number of threads: 5430
Number of posts: 16951

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Help with strncpy - crypt code Posted by szylvia on 14 May 2011 at 7:24 AM
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’s why 5.5.1 is ok but I accept any suggestion of everybody : )


I have a piece of code here that is burning my brain : (

The code is simple :

I put a word(or phrase - I do not want limitation ,but lets say here “abc” ) and my code will get each letter of this word and replace with my “Code[] array”

So lets say that
A=duL
B= 1TQ
And c = YTp

In the end of the function , instead of I have “abc” , I will get a string with “ duL1TQ YTp”. Later ,I have the decrypt function , which is going to write back to me “abc”.
However , I am facing 2 problems :


a) For all word that I write, always there is some “extra ASC/ANSI characters” in the begin of my string.
For example to “abc” , I get “asc-code abc” . I want just my “abc” ... : ( .

I think this issue is on line 123 when I write “strncpy (letter, &text[c], 3); ”...not sure , neither how to fix..


b) If I write a huge word p="abcdefghijklmnopqrstuvxz"; , it returns me just “asc code abcdefghijklmnop”. Nothing else. I do not want limitation... I want to write a huge phrase..

Thank you everybody and I hope I can help anyone here as well

Cheers!
Szilvia

Attachment: Crypt.cpp (4497 Bytes | downloaded 204 times)
Report
Re: Help with strncpy - crypt code Posted by pseudocoder on 15 May 2011 at 12:10 AM
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.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

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) = '\0';
   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;
}

Report
Re: Help with strncpy - crypt code Posted by szylvia on 15 May 2011 at 12:27 AM

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 :
“if((tmp = malloc((strlen(src) * 3) + 1)) != NULL) {“ ...
Could you please help me out with that?

Error:
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
Crypt.cpp:
Error E2034 Crypt.cpp 13: Cannot convert 'void *' to 'char *' in function encode(const char *)
Error E2034 Crypt.cpp 30: Cannot convert 'void *' to 'char *' in function decode(const char *)
*** 2 errors in Compile ***
** error 1 ** deleting Crypt.obj



Ps: Please do not be offended ,but If you have a paypal account, please send it to me , would be a pleasure send you some gift there for help me here..
Cheers!

Report
Re: Help with strncpy - crypt code Posted by szylvia on 15 May 2011 at 12:24 AM

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 :
“if((tmp = malloc((strlen(src) * 3) + 1)) != NULL) {“ ...
Could you please help me out with that?

Error:
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
Crypt.cpp:
Error E2034 Crypt.cpp 13: Cannot convert 'void *' to 'char *' in function encode(const char *)
Error E2034 Crypt.cpp 30: Cannot convert 'void *' to 'char *' in function decode(const char *)
*** 2 errors in Compile ***
** error 1 ** deleting Crypt.obj



Ps: Please do not be offended ,but If you have a paypal account, please send it to me , would be a pleasure send you some gift there for help me here..
Cheers!

Report
Re: Help with strncpy - crypt code Posted by pseudocoder on 15 May 2011 at 12:35 AM
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.

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


welcome to the board.
Report
Re: Help with strncpy - crypt code Posted by szylvia on 15 May 2011 at 1:05 AM
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 “int main” to my “GetCryptDecrypt”, which I want it returns me the “encoded” code, I get an error again : (

Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
Crypt.cpp:
Error E2034 Crypt.cpp 72: Cannot convert 'int' to 'const char *' in function __stdcall GetCryptDecrypt(int)
Error E2342 Crypt.cpp 72: Type mismatch in parameter 'lpText' (wanted 'const char *', got 'char') in function __stdcall GetCryptDecrypt(int)
Error E2034 Crypt.cpp 82: Cannot convert 'int' to 'char *' in function __stdcall GetCryptDecrypt(int)
*** 3 errors in Compile ***

I just changed it :


char* __export __stdcall GetCryptDecrypt(int ON){
//int main(void) {

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

MessageBox (0, " encrypted text======= ", "S",MB_OK);
MessageBox (0, *encoded, "S",MB_OK);

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

return (*encoded);
}




Thanks again , and I did not see your paypal account.. would be my pleasure to send you a gift, if you do not mind of course!
Cheers!
S

Report
Re: Help with strncpy - crypt code Posted by szylvia on 15 May 2011 at 1:28 AM
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



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.