Thread: Concatenate strings arrays

  1. #1
    Registered User
    Join Date
    Dec 2017
    Posts
    15

    Concatenate strings arrays

    Hi

    I have one big string array and I need to concatenate multipes strings arrays. Like this:

    char buffer[9][9];
    char texto_rol[9][500];

    texto rol= texto rol + buffer

    How to do this in C?

    My code:

    Code:
    include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    char buffer[9][9];
    char texto_rol[9][500];
    char texto[3];
    int a,b,c;
    int main()
    {
    //texto="ab a bc";
    strncpy(texto,"abba", 3);
    int a=0,b=0;
    for (a=0;a<strlen(texto)-2;a++)
    {
    escolher_caracter(texto[a], buffer);
    
    //for (b=0;b<8;b++)
    //{
    
    
            for (b=0; b<9; b++)
            {
                for (c=0; c<10; c++)
                   // for (c=0; c<strlen(buffer[0][b]); c++)
                {
               strcat(texto_rol, buffer[b][c]);
                }
            }
    
    
    
    //}
    }
    
        return 0;
    Code:
    void escolher_caracter( char letra, char buffer[9][9])
    {
        switch (letra){
    
        case 'SP': //espašo entre letras
            strncpy(buffer[0], "   ", 4);
            strncpy(buffer[1], "   ", 4);
            strncpy(buffer[2], "   ", 4);
            strncpy(buffer[3], "   ", 4);
            strncpy(buffer[4], "   ", 4);
            strncpy(buffer[5], "   ", 4);
            strncpy(buffer[6], "   ", 4);
            strncpy(buffer[7], "   ", 4);
            strncpy(buffer[8], "   ", 4);
            break;
            case ' ': //espašo entre palavras
            strncpy(buffer[0], "        ", 9);
            strncpy(buffer[1], "        ", 9);
            strncpy(buffer[2], "        ", 9);
            strncpy(buffer[3], "        ", 9);
            strncpy(buffer[4], "        ", 9);
            strncpy(buffer[5], "        ", 9);
            strncpy(buffer[6], "        ", 9);
            strncpy(buffer[7], "        ", 9);
            strncpy(buffer[8], "        ", 9);
            break;
        case 'a': //letra a
            strncpy(buffer[0], "   ##   ", 9);
            strncpy(buffer[1], "  #  #  ", 9);
            strncpy(buffer[2], "#      #", 9);
            strncpy(buffer[3], "#      #", 9);
            strncpy(buffer[4], "#      #", 9);
            strncpy(buffer[5], "########", 9);
            strncpy(buffer[6], "#      #", 9);
            strncpy(buffer[7], "#      #", 9);
            strncpy(buffer[8], "#      #", 9);
            break;
        case 'b':
            strncpy(buffer[0], "####### ", 9);
            strncpy(buffer[1], "#      #", 9);
            strncpy(buffer[2], "#      #", 9);
            strncpy(buffer[3], "#      #", 9);
            strncpy(buffer[4], "########", 9);
            strncpy(buffer[5], "#      #", 9);
            strncpy(buffer[6], "#      #", 9);
            strncpy(buffer[7], "#      #", 9);
            strncpy(buffer[8], "####### ", 9);
            break;
        default:
            strncpy(buffer[0], "########", 9);
            strncpy(buffer[1], "########", 9);
            strncpy(buffer[2], "########", 9);
            strncpy(buffer[3], "########", 9);
            strncpy(buffer[4], "########", 9);
            strncpy(buffer[5], "########", 9);
            strncpy(buffer[6], "########", 9);
            strncpy(buffer[7], "########", 9);
            strncpy(buffer[8], "########", 9);
            break;
        }
    
    }
    Last edited by totoco; 12-02-2017 at 06:19 AM.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Use strcat or strncat for concatenation. As a sidenote, there's no 'SP'. Multibyte characters are implementation-defined behavior, as you don't know how they would be encoded.
    Last edited by GReaper; 12-02-2017 at 06:33 AM.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    15
    My problema is to print a complete array. I as changed de main code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    char buffer[9][9];
    char texto_rol[9][500];
    char texto[3];
    int a,b,c;
    int main()
    {
    strncpy(texto,"abab", 3);
    int a=0,b=0;
    for (a=0;a<strlen(texto)+1;a++)
    {
    escolher_caracter(texto[a], buffer);
    strcat(texto_rol, buffer);
    }
    printf("\n %s",&texto_rol[0][0]);
    printf("\n %s",&texto_rol[1][0]);
    printf("\n %s",&texto_rol[2][0]);
    printf("\n %s",&texto_rol[3][0]);
    printf("\n %s",&texto_rol[4][0]);
    printf("\n %s",&texto_rol[5][0]);
    printf("\n %s",&texto_rol[6][0]);
    printf("\n %s",&texto_rol[7][0]);
    printf("\n %s",&texto_rol[8][0]);
    reurn 0;
    Concactente just one line. But if I print the buffer ther print the 8 lines for each caracter.

    I need help to print all lines of texto rol string array.
    And the answer is:

    Code:
        ##   #######    ##   ########
    
    
    
    
    
    
    
    
    Process returned 0 (0x0)   execution time : 0.050 s
    Press any key to continue.

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Well, your function thinks it's using a buffer that is 9x9, not 9x500. You should fix that.
    Devoted my life to programming...

  5. #5
    Banned
    Join Date
    Aug 2017
    Posts
    861
    look at comments in code.
    Code:
    // how many letters?
     strncpy(texto,"abab", 3);
    //what number did you give it to use?
    your two 2d array, What so they have in common? rolls and columns. so you need to figure out how to move both down and across in the proper sequence using what they have to access the rolls and columns keeping in mind it uses pointers.That means that both side are going to be needing to use the brackets.

    you are concatenating one "word" into another 2d array. filling that other 2d array's rolls with that 'word' . Think about how you print out a 2d array one roll at a time, then apply that methodology to your strcat. to the one that is being used to have that 'word' added to it on each of its (lines) rolls.

    your function Oh my my, what did you forget there, or what where you thinking when you wrote it?

    I see what you're trying to do. Getting all fancy on it. I do not know exactly what you're trying to do here, but it looks like copy each one of them case's into your texto_rol one to each (line) roll. so it prints out pretty much like it looks in the switch, perhaps with a box around the letters?

    Code:
    strncpy(texto,"abab", 3);
         printf("texto = %s\n", texto);
         len = strlen(texto);
         
        for ( b = 0; b < 9; b++)
        {printf("B = %d\n", b);
            for (a=0; a < len+1; a++)
            {  
                printf("len = %d\n", len);
                printf("sent to function %c, a = %d\n", texto[a], a);
                
                escolher_caracter(texto[a], buffer);
                strcat(texto_rol[b], buffer[a]);
                printf("B: sent to function %c, a = %d\n", texto[a], a);
    it is an excellent try, but it is going to fail when you get your 2d strcat figured out. Just a heads up.
    add these printf to your switch so when you get your loops figured out and run it you can see what it i doing when you call that function in that loop, I did have to make a slight adjustment to your SB case, it is invalid. it can only take one char/letter as pointed out beforehand. Keeping in mind that your strncpy(texto,"abab", 3); is not quite up to par either.

    Code:
    void escolher_caracter( char letra, char buffer[9][9])
    {
        switch (letra){
     
        case 's': //espašo entre letras
        printf("in switch s\n");
            strncpy(buffer[0], "   ", 4);
            strncpy(buffer[1], "   ", 4);
            strncpy(buffer[2], "   ", 4);
            strncpy(buffer[3], "   ", 4);
            strncpy(buffer[4], "   ", 4);
            strncpy(buffer[5], "   ", 4);
            strncpy(buffer[6], "   ", 4);
            strncpy(buffer[7], "   ", 4);
            strncpy(buffer[8], "   ", 4);
            break;
            case ' ': //espašo entre palavras
            printf("in switch space\n");
            strncpy(buffer[0], "        ", 9);
            strncpy(buffer[1], "        ", 9);
            strncpy(buffer[2], "        ", 9);
            strncpy(buffer[3], "        ", 9);
            strncpy(buffer[4], "        ", 9);
            strncpy(buffer[5], "        ", 9);
            strncpy(buffer[6], "        ", 9);
            strncpy(buffer[7], "        ", 9);
            strncpy(buffer[8], "        ", 9);
            break;
        case 'a': //letra a
        printf("in switch a\n");
            strncpy(buffer[0], "   ##   ", 9);
            strncpy(buffer[1], "  #  #  ", 9);
            strncpy(buffer[2], "#      #", 9);
            strncpy(buffer[3], "#      #", 9);
            strncpy(buffer[4], "#      #", 9);
            strncpy(buffer[5], "########", 9);
            strncpy(buffer[6], "#      #", 9);
            strncpy(buffer[7], "#      #", 9);
            strncpy(buffer[8], "#      #", 9);
            break;
        case 'b':
        printf("in switch b\n");
            strncpy(buffer[0], "####### ", 9);
            strncpy(buffer[1], "#      #", 9);
            strncpy(buffer[2], "#      #", 9);
            strncpy(buffer[3], "#      #", 9);
            strncpy(buffer[4], "########", 9);
            strncpy(buffer[5], "#      #", 9);
            strncpy(buffer[6], "#      #", 9);
            strncpy(buffer[7], "#      #", 9);
            strncpy(buffer[8], "####### ", 9);
            break;
        default:
        printf("in switch default\n");
            strncpy(buffer[0], "########", 9);
            strncpy(buffer[1], "########", 9);
            strncpy(buffer[2], "########", 9);
            strncpy(buffer[3], "########", 9);
            strncpy(buffer[4], "########", 9);
            strncpy(buffer[5], "########", 9);
            strncpy(buffer[6], "########", 9);
            strncpy(buffer[7], "########", 9);
            strncpy(buffer[8], "########", 9);
            break;
        }
     
    }
    Last edited by userxbw; 12-02-2017 at 10:47 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Concatenate strings in c
    By programinproces in forum C Programming
    Replies: 1
    Last Post: 11-28-2013, 04:02 PM
  2. Using Concatenate strings to communicate to a client
    By David25 in forum Networking/Device Communication
    Replies: 1
    Last Post: 09-30-2012, 01:27 AM
  3. Concatenate two strings.
    By ModeSix in forum C Programming
    Replies: 21
    Last Post: 04-26-2011, 10:12 AM
  4. concatenate two strings! K&R problem
    By karanmitra in forum Linux Programming
    Replies: 2
    Last Post: 08-09-2005, 10:44 AM
  5. Concatenate chars and std::strings.
    By Hulag in forum C++ Programming
    Replies: 3
    Last Post: 06-29-2005, 08:20 AM

Tags for this Thread