Home > OS >  How to get 2d array from a string (whole sentence) (each word a separate row)?
How to get 2d array from a string (whole sentence) (each word a separate row)?

Time:12-12

I've got some homework for my university subject, this is just a one part of the code that I don't know what is the most efficient way that I could use in the future and also to understand it fully.

I am new to programming so I still do not have that logic with things like this, one thing that is helping my is white board on which I "test" program to see what happens, it helped me for some things. (This is my first post, not sure if it is writed the right way)

I tried to make it with nested loop, with the if condition whenever it finds blank space, it starts with new row and continue to read from string. Not sure if I've done it right but I'd like to see how would you do it so.

I can write my code here but everything is named in my language, this is part of that function:

brojac = counter

br_beline = blank space

pamcenje = variable to continue from where J lasted in i = 0; so when i = 1, j starts from last for it has been

rnk = char array, the string from input

novi = 2d array that I want to make it

int orf(char rnk[]) {
   int i,j;
   int brojac = 0;
   int pamcenje = 0;
   char novi[MAKS][MAKS] = {}; //MAKS IS 100
   int br_beline = brojac_belina(rnk);
   for (i = 0; i <= br_beline;   i, brojac = 0){
        for (j = pamcenje; brojac < 1;   j,   pamcenje){
             if (rnk[j] != ' ' & rnk[j] != NULL) {
                  novi[i][j] = rnk[j];
                  pamcenje = j;
              }
              else{
                   brojac;
                 pamcenje = j;
              }
        }
   }

CodePudding user response:

If it's available to you, you can use string.h strtok function to separate words in a sentence and store them in an array, it's ready to use out of the box and it greatly simplifies matters, here is an example:

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

int main() {
 
    char sentence[] = "This is some string";  

    // array good for 100 words with 49 characters each
    // make sure it has enough space for all the words in sentence
    char(*words)[50] = malloc(100 * sizeof(*words));

    char* word;
    word = strtok(sentence, " ");

    int word_count = 0;

    while (word != NULL) {
        strcpy(words[word_count], word); //copy the word to the array
        word_count  ;
        word = strtok(NULL, " ");
    }

    // test print
    for (int i = 0; i < word_count ; i  ) {
        printf("%s\n", words[i]);
    }
    // notice that word_count is the number of words stored, so you don't want to lose that
    // we could use a sentinel but let's not complicate matters

    free(words);
}

See it live: https://godbolt.org/z/a5fnc1EKb

CodePudding user response:

strtok() is, as others have written, probably the solution.

Here's another that is pretty trivial, if you want to work with every byte in your code.

#define MAKS 100
void orf( char rnk[] ) {
    char novi[MAKS][MAKS];

    int i = 0, j = 0, k = 0;
    while( rnk[k] ) {
        while( rnk[k] && rnk[k] == ' ' ) k  ;
        while( rnk[k] && rnk[k] != ' ' ) novi[ i ][ j   ] = rnk[ k   ];
        novi[ i   ][ j ] = '\0';
        j = 0;
    }
    for( int x = 0; x < i; x   )
        puts( &novi[ x ][ 0 ] );
}

int main() {
    orf( "The quick brown fox jumps over the lazy dog" );
    return 0;
}
The
quick
brown
fox
jumps
over
the
lazy
dog

It's good to learn. And, sometimes, just using 'generic' variable names may help others to understand the code much more easily.

  • Related