Thread: append character to array

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    28

    append character to array

    Quick question all, how do you append a character to an array? Say for example I prompt the user for a string and store that value in a array.
    later on, I accept another value(a key) and store that value in a array.
    I want the length of the first array to equal to the length of the key.

    For example:

    daryl--plaintext
    Kyle Peterson-Key value

    I want (daryl) to repeat until it equals the length of Kyle Peterson.But I want to append it to an existing array. How do I do that? suggestions would be appreciated. Thanks!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What ideas do you have in mind?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    28
    Quote Originally Posted by laserlight View Post
    What ideas do you have in mind?
    In other words, where is my code?

    The code I have looks like this:
    Code:
    a=e //where e is the length of plain text string
    m=//length of key string
    for int i=a;i<m, i++)
    
    {
        plaintext[a] =plaintext[i];
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Not really: ideas and code aren't exactly the same thing. I'm asking you to describe some idea that you have to solve this, perhaps outline an algorithm.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Dec 2012
    Posts
    28
    Quote Originally Posted by laserlight View Post
    Not really: ideas and code aren't exactly the same thing. I'm asking you to describe some idea that you have to solve this, perhaps outline an algorithm.
    I see, but the code I have would not work?

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    505
    You need to use dynamic memory.

    Start with the empty string (not to be confused with NULL)

    Code:
    int len = 0;
    char *str = malloc(1);
    str[0] = 0; /* the string is just a nul character, so it's empty */
    Now to add a letter

    Code:
    char *temp;
    char ch = 'x'; /* the new character */
    
    temp = realloc(str, len + 2);
    if(temp == 0)
      /* we're out of memory, handle this */
    str = temp;
    len = len +1;
    str[len-1] = ch;
    str[len] = 0; /* don't forget to add the nul */
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by saldar05
    I see, but the code I have would not work?
    It won't: besides being syntactically incorrect, you are continually assigning to the same character, but you actually want to be assigning to different positions in the array.

    Quote Originally Posted by Malcolm McLean
    You need to use dynamic memory.
    Maybe, maybe not. The plaintext array could simply be made to be of the same size as the key array.

    Quote Originally Posted by Malcolm McLean
    Start with the empty string (not to be confused with NULL)
    (...)
    Now to add a letter
    You're always increasing the dynamic array size by a fixed amount, which is not a good thing once the array gets bigger since you will be making allocations that could have been avoided. Besides, I think that this should wait: if saldar05 cannot articulate an algorithm to repeat the string just yet, involving dynamic memory allocation is just jumping the gun.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Dec 2012
    Posts
    28
    I have an idea of what to do now. I see my problem. if I did this:
    Code:
    =e //where e is the length of plain text string m=//length of key string
    tempvalue  =m-e
     
    for int i=0;i<tempvalue, i++) 
    
    { 
    plaintext[e] =plaintext[i]; 
    }
    That would allow me to to repeat the character at the location i after the location e in plaintext array. Correct?

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I think e is being misused. For one thing, it's relatively constant. You'll be able to see the issue here:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define TEXT_SIZE 32
    /* TEXT_SIZE was specifically chosen to be (significantly) longer than both strings. */
    
    int main(void) {
        char plaintext[TEXT_SIZE] = "daryl";
        char keytext[TEXT_SIZE] = "Kyle Peterson";
    
        size_t e = strlen(plaintext);
        size_t m = strlen(keytext);
    
        size_t difference = m - e;
        size_t i;
        for (i = 0; i < difference; i++) {
            plaintext[e] = plaintext[i];
            printf("%s\n", plaintext);
       }
       return 0;
    }
    /* my output:
    daryld
    daryla
    darylr
    daryly
    daryll
    daryll
    daryl
    daryl
    */
    Not quite what you had in mind. If you want to end up with "daryldaryl" or something like that, you're going to have to move e past the "end" and keep copying from cells 0 to 4 inclusive. I think that you will need to use a nested loop as well. The inner loop will do the appending, and the outer loop will keep track of how many times "daryl" is supposed to repeat.
    Last edited by whiteflags; 12-28-2012 at 10:22 PM.

  10. #10
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    If you can guarantee that the arrays are big enough to fit the key string then you might not need to do dynamic allocation. It depends -- the "size" of the array will be the size it was declared with, but the string length can vary independently.

    Code:
    =e //where e is the length of plain text string m=//length of key string
    tempvalue  =m-e
     
    for int i=0;i<tempvalue, i++) 
    
    { 
    plaintext[e] =plaintext[i]; 
    }
    I think I can see what you're trying to do, maybe. If you recalculate the length of plaintext each time through the loop, it'll work. However it'll only work if you have a reliable way of working out the length of the string. strlen() counts the characters up until the NULL terminator, which you'll overwrite with your copy code.

    So you could:
    Code:
    for (int i=0;i<tempvalue, i++) 
    { 
        e = strlen(plaintext);
        plaintext[e] =plaintext[i]; 
        plaintext[e+1] = 0;
    }
    That's not a very efficient way of doing it though -- calling strlen every time through the loop. It's not necessary.

    Say your plaintext is "hello". Then you want to copy element 0 to element len(hello), element 1 to len(hello)+1, element 2 to len(hello)+2 and so on. The difference between where you copy from and to is always the same: it's the original length of plaintext.

    Don't forget that you'll still need to NULL terminate the new plaintext.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do I remove a character from character array?
    By nick753 in forum C++ Programming
    Replies: 25
    Last Post: 12-08-2010, 11:27 AM
  2. Replies: 15
    Last Post: 09-23-2010, 02:19 PM
  3. REmoving a character from a character array
    By Bladactania in forum C Programming
    Replies: 3
    Last Post: 02-11-2009, 02:59 PM
  4. Replies: 8
    Last Post: 11-12-2008, 11:25 AM
  5. Replies: 7
    Last Post: 05-11-2008, 10:57 AM