0

Suppose I accept an input that is a string "I am a beginner in programming". What I want to do is store that input per word into a 2d array. How do I translate that to code?

I am new to programming so I am not really that familiar with it.

I tried coding it this way and perhaps the error is in the inner loop part. I think it repeats storing the "I" after it breaks out from the inner loop and having variable col initialized again? But I can't really think of a way to correct it.

int main(void) {
    const int SIZE1=20;
    const int SIZE2=30;
    char string[100];
    char string2[SIZE1][SIZE2];
    int row, col;

    printf("Input a string: ");
    gets(string);

    for(row=0; row<SIZE1; row++)
    {
        for(col=0; col<SIZE2; col++)
        {
            if(string[col]==' ')
                break;
            else    
                string2[row][col]=string[col];
        }
    }

    printf("%s\n", string2[0]);
    printf("%s\n", string2[1]);
    printf("%s\n", string2[2]);
    printf("%s\n", string2[3]);
    printf("%s\n", string2[4]);
    printf("%s\n", string2[5]);
    printf("%s\n", string2[6]);

    return 0;
}
Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
Beginner
  • 11
  • 1
  • @AndreasWenzel Copying the individual words from the input string to the 2D array. – Beginner May 30 '22 at 09:13
  • 1
    Side note: [Why is the gets function so dangerous that it should not be used?](https://stackoverflow.com/q/1694036/12149471) – Andreas Wenzel May 30 '22 at 09:30
  • Oh, I never knew the gets function was dangerous. Thanks :) – Beginner May 30 '22 at 09:33
  • Side note: The name for the 2D array `string2` is misleading, as that name implies that it is a single string. You may want to rename it to `strings`, to make it clear that it it represents more than one string. – Andreas Wenzel May 30 '22 at 09:37
  • If you add the necessary `#include` directives, then it will be easier for other people to test your program. Otherwise, people who want to test your program will have to guess which ones are required and add them themselves. – Andreas Wenzel May 30 '22 at 09:45
  • **I am voting to reopen the question because** the alleged duplicate question is not an appropriate duplicate, in my opinion. The alledged duplicate question effectively asks how to copy a string. However, in the question at hand, we have the additional issue that not all characters up to the terminating null character should be copied, but only a certain number of characters (up to the word boundary) should be copied. Since this issue is not addressed in the alleged duplicate question, I believe both questions are substantially different. – Andreas Wenzel May 30 '22 at 10:00
  • @Lundin: For the reason stated in my previous comment, I disagree with your decision to close the question as a duplicate. – Andreas Wenzel May 30 '22 at 10:04
  • The logic of your nested loops is wrong, for two reasons: 1. You are not writing a terminating null character to the strings. 2. You want the outer loop to stop after reaching the end of the input string, and not when 20 words have been written to the 2D array. [Here](https://godbolt.org/z/ovf64jMPh) is my solution to the problem. I cannot post it as an answer, because the question has been closed and not (yet) reopened. – Andreas Wenzel May 30 '22 at 11:12

1 Answers1

0

You can use strtok function which breaks string into a series of tokens using the delimiter (A Space Character in this case).

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

int main(void) {
    char myText[] = "I am a beginner in programming";

    char *p = strtok(myText, " "); // Get The First Token
    char *myTextArr[6];

    int i = 0;
    while (p != NULL) {
        myTextArr[i++] = p;
        p = strtok(NULL, " "); // Walk through other tokens
    }

    for (i = 0; i < 6; ++i) {
        printf("%s\n", myTextArr[i]);
    }

    return 0;
}

Output:

I
am
a
beginner
in
programming
Aditya
  • 1,132
  • 1
  • 9
  • 28