Repeating a String in C

This is a discussion on Repeating a String in C within the C Programming forums, part of the General Programming Boards category; I have an encryption assignment that takes input to create a key and another input to create a message, then ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    6

    Repeating a String in C

    I have an encryption assignment that takes input to create a key and another input to create a message, then taking the values of the key, it adds them to the message in such a way that the message will become a code. So if the message is "crow" and the key is "abcd" the new message, or code will be "csqz"

    I have gotten everything to work correctly except that I run into problems if the message is longer than the key. Last week I had the same type of assignment but we got to write it in C++ so to make sure the key would repeat itself until it was as long as the message I did this:

    Code:
    while (s.length() > k.length())
            {
                k = k+k;
            }
    *note, k is the string that has been chosen through user input as the key
    * s.length is the number of characters in the message and k.length is the number of characters in the key

    However, I tried the same type of thing in C and it does not work:

    Code:
    while (keyLength<messageLength)
            {
                key = key + key;
            }
    *note, key is the string representing the input key and message is the string representing the input message

    I get the compiler error (I'm using GCC):

    "invalid operands to binary +"

    If anybody has a solution that would fix this problem that would be great.

    Thanks

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,263
    Quote Originally Posted by Keegan View Post
    However, I tried the same type of thing in C and it does not work:
    Nor should it. I don't think this is a very good method, anyway. You don't need to duplicate the key over and over. When you hit the last character of the key just go back to the beginning again.

  3. #3
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    I gather key is a string...?

    I'm not sure which one you wanted as a counter, 'keyLength' or 'messageLength'.

    But it should look something like:
    Code:
    char k[128];    /* whatever */
    /* ... */
    size_t i = 0;    /* our counter */
    size_t kLength = strlen(k);
    
    while(i < kLength)
    {
        k[i] += k[i];
        ++i;
    }
    I think I've totally missed the point, nvm.

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    6
    Quote Originally Posted by brewbuck View Post
    Nor should it. I don't think this is a very good method, anyway. You don't need to duplicate the key over and over. When you hit the last character of the key just go back to the beginning again.
    Is there any way to repeat the string though, because for the second part of the assignment I'm going to have to change the letter of the key I am on to the next letter of the alphabet if the cyphertext is a character greater than J. Repeating the string would make it much easier than starting back at the beginning of the key again.

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,263
    Quote Originally Posted by Keegan View Post
    Is there any way to repeat the string though, because for the second part of the assignment I'm going to have to change the letter of the key I am on to the next letter of the alphabet if the cyphertext is a character greater than J.
    So just keep track if the last ciphertext letter was greater than J, and if so add 1 to the next ciphertext.

  6. #6
    Registered User
    Join Date
    Nov 2007
    Posts
    6
    Quote Originally Posted by brewbuck View Post
    So just keep track if the last ciphertext letter was greater than J, and if so add 1 to the next ciphertext.
    I can do that, but it doesn't add one to the next cyphertext, it adds one to that character in the key, so if the cyphertext that was calculated from the letter c in the key "cat" was greater than J, then the key next time would be "dat"

    I should have been more specific, but if I could repeat the key the way I've been trying, I think I could get this to work.

  7. #7
    Registered User
    Join Date
    Nov 2007
    Posts
    6
    Also, if I am going to have to go back to the beginning of the key each time, could you exlpain how to do that?

    I assume it would be something similar to
    Code:
    if(key[i] == 0)
    {
      i = 0
    }
    and have a counter in there to make sure that it will stop encrypting when I get to the end of the message.

    The reason I have key[i] = 0
    is because 0 is the last character in any string so if the character at that i position in the key is 0 it means I've reached the end of the string and should repeat it.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    In C, the equivalent of k = k + k would be something like:
    Code:
    /* First ensure k is big enough for the operation */
    size_t kLength = strlen(k);
    for (i=0; i<kLength; i++)
    {
        k[i+kLength] = k[i];
    }
    k[i+kLength] = '\0';

  9. #9
    Registered User
    Join Date
    Nov 2007
    Posts
    6
    Quote Originally Posted by swoopy View Post
    In C, the equivalent of k = k + k would be something like:
    Code:
    /* First ensure k is big enough for the operation */
    size_t kLength = strlen(k);
    for (i=0; i<kLength; i++)
    {
        k[i+kLength] = k[i];
    }
    k[i+kLength] = '\0';
    Thank you so much, but I just tried it BrewBuck's way of going back to the beginning of the string when I'm done, and it worked. It took a good half hour to figure out how to change the encryption process as well, but I got everything I think. Thanks a lot.

  10. #10
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,263
    Quote Originally Posted by swoopy View Post
    In C, the equivalent of k = k + k would be something like:
    Code:
    strcat(k, k);
    Bwahahaha!

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934

    Thumbs up

    Quote Originally Posted by brewbuck View Post
    Code:
    strcat(k, k);
    Bwahahaha!
    That's a nice example of undefined behavior.

  12. #12
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,263
    Quote Originally Posted by swoopy View Post
    That's a nice example of undefined behavior.
    Thus my evil mad scientist laugh.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 07:02 AM
  2. Replies: 8
    Last Post: 04-25-2008, 03:45 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 07:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21