2

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
6
  • And what do you get when you read array[1][[0] and array[0][1]? Your question isn't very clear. Commented Feb 15, 2011 at 18:46
  • can u please specify why you need this? And what should you get when you read arr[1][0]? Commented Feb 15, 2011 at 18:48
  • i want to make a 2D array from a string.. Commented Feb 15, 2011 at 18:54
  • Did you intend to put spaces between all the letters in the final array? Commented Feb 15, 2011 at 19:09
  • No never...it is just for readability Commented Feb 15, 2011 at 19:12

3 Answers 3

2

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)];
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

@Robertew: array[0] is the first array in your 2d array. array[0][0] is the first character of that first array.
@Robertw: Yep, that's what I described.
0

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.