Renjith G
Renjith G

Reputation: 4868

How can I write a string into a two dimensional array in C?

May I know how I can a write a character string into a 2D character array? I need to read every character in the string and put it into a 2D array.

For example:

char string[10];

I want to write all the characters in the string into a 2D array.

That means, when I read array[0][0], I should get the first character.

Update:

suppose my string is "GOODMORN" then the 2D array should be look like this..

  0|1|2|3
0 G|O|O|D
1 M|O|R|N

Upvotes: 2

Views: 17881

Answers (3)

rubber boots
rubber boots

Reputation: 15184

Disclaimer: I didn't really understand what the question is about ;-)

you could simply copy the string into a location pointed to by the array constant for the 2D array:

...

char array[2+1][4];
memcpy((void *)array, TEXT, sizeof(TEXT));

...

But this wouldn't yield auto-sizeable arrays. Maybe you think of the following:

#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <stdio.h>

 char **matrixify(char text[], int vert, int horiz)
{
 int i=0;
 char **m = (char **)calloc(vert, sizeof(char*));
 do  m[i] = text + i * horiz; while(i++ < vert); 
 return m;
}

 int main()
{
 int x, y;
 /* char t[] = "this is a really long text with many words and stuff"; */
 char t[] = "GOODMORN";
 int edge = 1+(int)sqrt((double)strlen(t)); /* make a square from text */
 /* int vert = edge, horiz = edge; */ /* Auto size detection */
 int vert = 2, horiz = 4;

 char *textbuf = (char *)calloc(vert, horiz); /* not always 0-terminated */
 char **matrix = matrixify(strncpy(textbuf, t, vert*horiz), vert, horiz);

 for(y=0; y<vert; y++) {
   for(x=0; x<horiz; x++) printf("%c ", matrix[y][x]);
   printf("\n");
 }
 /*  prints:
   G O O D
   M O R N
   through matrix[i][j]   */
 return 0;
}

which leads to your memory layout but looks complicated for the problem state, but it's C ...

regards

rbo

Upvotes: 0

Giuliano
Giuliano

Reputation: 192

A sample program I just wrote for you to play with and see if this is what you want. The important part is where the loops enter the game, dividing the strings character by character.

There are a lot of improvements that can be done (strncpy, the input variable being dynamic, MEMORY FREES, etc), but this is up to you.

Edit: The strncpy modification was just posted by rubber boots.

int main()
{
    char A[12] = "Hello World", **B;
    int B_LEN = strlen(A) / 2 + 1;

    B = (char**)malloc(2 * sizeof(char*));
    B[0] = (char*)malloc(B_LEN * sizeof(char));
    B[1] = (char*)malloc(B_LEN * sizeof(char));

    int i, j;

    for (i = 0; i < 2; i++) {
        for (j = 0; j < B_LEN; j++) {
            B[i][j] = A[B_LEN * i + j];
        }
        B[i][j] = '\0';
    }

    printf("%s", B[0]);
    printf("[END]\n");
    printf("%s\n", B[1]);
    printf("[END]\n");

    return 0;
}

Obs.: The output must be like

Hello [END]
World[END]

The tag is to show you wether there are spaces, i.e., where exactly the division happened.

Upvotes: 0

nmichaels
nmichaels

Reputation: 50943

First, make sure array[0] is big enough to hold your string. Second, use memcpy or strncpy to copy the bytes of string into array[0].

If you need to process and address each character individually, you can start by doing what memcpy does, but in a for loop:

#define NUM_ARRAYS 2
#define LENGTH 4

char *string = "GOODMORN";

for (arr = 0; arr < NUM_ARRAYS; arr++)
{
    for (idx = 0; idx < LENGTH; idx++)
    {
        array[arr][idx] = string[idx + (arr * LENGTH)];
    }
}

Upvotes: 2

Related Questions