Thread: strlen

  1. #1
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718

    strlen

    Can anybody see why strlen(Encrypted) should return 24?
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    
    void XOREncrypt(const char*, const char*, char*);
    
    void XOREncrypt(const char* Text, const char* Key, char* Out)
    {
    	long CurIndex = 0, KeyIndex = 0;
    	delete[] Out;
    	Out = new char[strlen(Text)];
    	for(CurIndex = 0; CurIndex < strlen(Text); CurIndex++)
    	{
    		Out[CurIndex] = Text[CurIndex] ^ Key[KeyIndex];
    		KeyIndex++;
    		if(KeyIndex == strlen(Key))
    			KeyIndex = 0;
    	}
    }
    
    int main()
    {
    	char* Encrypted = new char;
    	char Text[] = "testing", Key[] = "xSquared";
    	XOREncrypt(Text,Key,Encrypted);
    	printf("%s XOR %s = %ld", Text, Key, strlen(Encrypted));
    	getch();
    	return 0;
    }
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    strlen() looks for a string terminator to determine a string's length. So for strlen() to work, you need something like:

    Out[strlen(Text)] = '\0';

    at the end of XOREncrypt().
    Also, you need to either pass Out as a reference, or make it the return value.

    One other problem is if you xor a letter with the same letter, you get 0, which is the same as a string terminator.
    Last edited by swoopy; 07-15-2002 at 12:44 PM.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Okay, first thing. You don't need a function prototype if the function body is *before* main. Just FYI. Anyways, why in main are you only allocating 1 byte for Encrypted? You should probably allocate this elsewhere.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Swoopy is precisely correct. Depending on the results of the encryption, calling strlen would do one of three things:

    (1) You would get "lucky" and get the correct length.
    (2) A zero in the middle of the string would fool strlen into thinking there were less characters.
    (3) Strlen cruises straight past the array bounds and possibly raises an exception/crashes the computer!

    So store the length of the first function parameter somewhere and consider the resultant "encrypted" array as "non-text", never invoking string-based functions upon it.

    Two more suggestions: there is no need to allocate for the char in main like so. Just leave it be. Second, you call strlen in your function EVERY iteration of the loop, making it inefficient, especially if you are processing large chunks of memory.

    Overall, I like your style, though. Good work.






    Code:
    
    char* XOREncrypt(const char* Text, const char* Key)
    {
    long CurIndex = 0, KeyIndex = 0, TextLen = strlen(Text), KeyLen = strlen(Key);
    char *Out = new char[TextLen + 1];
    	for(CurIndex = 0; CurIndex < TextLen; CurIndex++)
    	{
    		Out[CurIndex] = Text[CurIndex] ^ Key[KeyIndex];
    		KeyIndex++;
    		if(KeyIndex == KeyLen)
    			KeyIndex = 0;
    	}
    Out[CurIndex] = 0; /*...null-term, so you can safely call string funcs (strlen, etc...)!
    But remember - even so, that may not function as expected...*/
    
    return Out;
    }
    
    
    
    
    int main()
    {
    
    	char Text[] = "testing", Key[] = "xSquared";
    	char* Encrypted = XOREncrypt(Text,Key);
    	printf("%s XOR %s = %s", Text, Key, Encrypted);
                    printf("Unreliable length: %i", strlen(Encrypted)); 
    	getch();
    	return 0;
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Thanks for your help. I've been programming since grade 5 (I'm now going into gr 11), when I taught myself QBasic. I've taught myself 10 programming languages so far, VB I learned last summer, and I've begun to teach myself C++ over the last two weeks.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Hmm, so is programming taught to youth in Canadian schools?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    Sir Mister Insane Sako Klinerr1's Avatar
    Join Date
    May 2002
    Posts
    608
    i taught myself html, qbasic, c++, acitonscript, javascript, php, and java, in scool im doing a viula basic class (seventh grade)
    Email: [email protected] || AIM: MisterSako || MSN: [email protected]

    -the shroom has spoken

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    You may already know this, but here are the changes to make.
    Code:
    >void XOREncrypt(const char*, const char*, char*);
    void XOREncrypt(const char*, const char*, char* &);
    
    >void XOREncrypt(const char* Text, const char* Key, char* Out)
    void XOREncrypt(const char* Text, const char* Key, char* &Out)
    >{
    >	long CurIndex = 0, KeyIndex = 0;
    >	delete[] Out;
    >	Out = new char[strlen(Text)];
    	Out = new char[strlen(Text)+1];
    >	for(CurIndex = 0; CurIndex < strlen(Text); CurIndex++)
    >	{
    >		Out[CurIndex] = Text[CurIndex] ^ Key[KeyIndex];
    >		KeyIndex++;
    >		if(KeyIndex == strlen(Key))
    >			KeyIndex = 0;
    >	}
            Out[CurIndex] = '\0';
    >}
    As far as the using strlen(), it's fine. Just make sure there are no matching characters in the same positions for Text and Key. Your example had no matching characters, so you probably already knew this.
    Last edited by swoopy; 07-16-2002 at 06:45 PM.

  9. #9
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >Hmm, so is programming taught to youth in Canadian schools?

    At my school, we learn VB in grade 11, and Java in grade 12. I'm going into grade 11 and taking gr12 programming and math, which really screwed up my schedule. =(
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strlen help
    By stewie1986 in forum C Programming
    Replies: 10
    Last Post: 12-04-2007, 12:15 PM
  2. strlen in expressions
    By justforthis1 in forum C++ Programming
    Replies: 4
    Last Post: 10-24-2006, 10:28 AM
  3. strlen()
    By exoeight in forum C Programming
    Replies: 9
    Last Post: 04-01-2005, 10:18 AM
  4. Just say NO to strlen.
    By anonytmouse in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 02-11-2005, 01:34 PM
  5. strlen
    By dirgni in forum C++ Programming
    Replies: 6
    Last Post: 12-08-2002, 11:57 PM