This is a unique triangle and is different from all other triangles in this way that it prints words separated by spaces.The answers i'm looking for are not there in any other question i've already checked.
the output should look like this
this
this is
this is the
this is the best
this is the best way
this is the best way to
this is the best way to spend
this is the best way to spend time
so far the code i have is
#include <stdio.h>
int main()
{
char msg[]="this is the best way to spend time for reedaf";
int inn=1, out, i=0, max;
max=(sizeof(msg)/sizeof(int))+1;
char *output;
char *spc=" ";
output=strtok(msg,spc);
for(out=1;out<max;out++){
for(i=0;i<out && output != NULL ;i++){
printf("%s ", output);
output=strtok(NULL,spc);
}
printf("\n");
}
return 0;
}
this generates this output
this
is the
best way to
spend time for reedaf
so please help me i cant find peace of mind
i need to start each line with the starting word in the array. then the next line should start with the starting word in the array then the next word. then the next line should again start with the starting word in the array then the next word and then the next word.and so on and so forth.
PLEASE TRY NOT TO USE break; OR memcpy
OP's use of strtok()
does not restore the string as needed, but simply marches down the string.
Follows is a candidate simplification.
void printTri(char *s) {
for (size_t i = 0; s[i]; i++) {
if (s[i] == ' ') {
s[i] = '\0';
puts(s);
s[i] = ' ';
}
}
puts(s);
}
int main(void) {
char msg[] = "this is the best way to spend time for reedaf";
printTri(msg);
return 0;
}