2 dimension string array

This is a discussion on 2 dimension string array within the C Programming forums, part of the General Programming Boards category; how can i insert a character on a 2 dimension string array for example if i have to insert "d" ...

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    20

    Red face 2 dimension string array

    how can i insert a character on a 2 dimension string array
    for example if i have to insert "d" on check[0][5].. i have tried the following way but after inserting 5 on check[0][5] it deletes all the elements which are after that index no.. please help me

    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    void main(void)
    {
    char check[1][80]={"............................................... ..............................."};
    clrscr();
    printf("%s\n",&check[0]);
    strcpy(&check[0][5],"d");
    printf("%s",&check[0]);
    getch();
    }
    Life is Like a Game.. You Just Dont Get 3 lives

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You can't just insert a character into the array, you need to move all the remaining characters up by one as well.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    20
    a small example will really help me .. can you please show me an example
    Life is Like a Game.. You Just Dont Get 3 lives

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    A "small" example would probably end up being the complete function, so here's some pointers instead:

    Your function prototype might look like this:
    >>insert (char *s, int InsertOffSet, char c)
    and you'd call it like so:
    >>insert (check[0], 4, 'd');

    Now within the function, you need to store away the existing character at offset 4 into a temp variable, then you can replace it with the new character.

    Then, store away the next character in the string, and then replace it in the string with the stored value from above.

    Follow the same principle until you reach the end of the string, where you then add a new trailing \0.

    Draw it out on paper if you get stuck.

    From your code you've already posted:

    >>printf("%s\n",&check[0]);
    This should be
    >>printf("%s\n",check[0]);

    >>void main(void)
    Read this
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,156
    The other thought is that if you mean by "insert" you simply want to replace whatever is in check[0][5] is to be overwritten by 'd' then the command is simply
    check[0][5] = 'd';

    What you did with strcpy(&check[0][5],"d");
    is replace the 'string' starting at position 0,5 with the string "d". In other words,
    check[0][5] = 'd';
    check[0][6] = '\0';
    which effectively truncates the string you had in check[0]

    Walt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  2. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 02:40 AM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

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