Programming Language: C
How can I add each word in a sentence in to a 2d char array?
Here's what I have so far:
char *text;
text = "This is my text";
char wordArray[500][500];
int textLength = strlen(text);
---------------
Sample output:
This
is
my
text
input code:
output:
code:
#include <stdio.h>
#include<string.h>
int main()
{
/*declare the variables*/
char *text;
text = "This is my text";
char wordArray[500][500];
int textLength = strlen(text),i=-1,j=0,k=0;
/*take loop for break string and store into std::array<T, N>
;*/
while(i<textLength)
{
/*initialize tha variables*/
i++;
k=0;
/*take loop*/
while(text[i]!=' '&&text[i]!='\0')
{
/*store into array*/
wordArray[j][k]=text[i];
i++;
k++;
}
/*add null value*/
wordArray[j][k]='\0';
j++;
}
/*print 2d array*/
for(int i=0;i<j;i++)
{
printf("%s\n",wordArray[i]);
}
return 0;
}
Get Answers For Free
Most questions answered within 1 hours.