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;
}