I'm trying to output a series of lines using a printf statement within nested for loops. The properly formatted output should have the very last argument of the printf statement, which is just a number in brackets at the end of each line, stay on that same line, but in my case, the number always goes to the next line. Here's my code:
for(i = 0; i < wordCount_keyword; i++) {
for(j = 0; j <= lineCount; j++) {
if(strstr(inputLines[j], keywords[i]) != NULL) {
printf("%-20s %s (%d)\n", keywords_upper[i], inputLines[j], j+1);
}
}
}
Here's the output I'm getting:
CAT the fish a dog cat dog rabbit
(1)
CAT the fish and cat
(2)
DOG the fish a dog cat dog rabbit
(1)
ELEPHANT a rabbit or elephant
(3)
FISH the fish a dog cat dog rabbit
(1)
FISH the fish and cat
(2)
RABBIT the fish a dog cat dog rabbit
(1)
RABBIT a rabbit or elephant
(3)
Here's the correct output that I should be getting:
CAT the fish a dog cat dog rabbit (1)
CAT the fish and cat (2)
DOG the fish a dog cat dog rabbit (1*)
ELEPHANT a rabbit or elephant (3)
FISH the fish a dog cat dog rabbit (1)
FISH the fish and cat (2)
RABBIT the fish a dog cat dog rabbit (1)
RABBIT a rabbit or elephant (3)
What am I doing wrong?