I'm a little stumped on a section of code in my program. I'm trying to take in a string of chars and then store each word within that string separately in an array of strings without using stdtok and then output them with only a single space separating each word, even if the two words were originally separated by more than one space in the input.. I have most of the code but I can't figure out how to how to load the string array after I add a null char after each word. Having a brain fart... Please help.
#include<stdio.h>
#include<string.h>
int main (void)
{
int i, j=0, count=0, length=0;
char array_char[1023];
char* array_str[512];
fputs("Type a string of words and then press 'Enter': ", stdout);
fflush(stdout);
fgets(array_char, sizeof array_char, stdin);
length = strlen(array_char); // add (-1) to delete the newline
printf(array_char);
printf("\n");
for(i=0; i<length; i++)
{
if(array_char[i] == ' ')
{
array_char[i] = '\0';
do
{
i++;
}
while(array_char[i] == ' ');
}
else
{
continue;
}
}
//add code here (without using stdtok) that loads each word in the string (until it encounters a null) into an array of strings.
//increment 'count' each time
for (i = count; i >= 0; i--) //yes I'm outputting them in reverse on purpose
{
printf("%s ", array_str[i]);
}
printf("\n");
return 0;
}