-
End of Line character
I'm scanning in a line of words and need to stop at the end of a line from a data file.
push this is the first line
pop 4
push this is the next line
pop 21
print
push good luck
push this is the last assignment for the semester
pop 15
print
delete_stack
the first word is what to do with the next set of data. Push words onto a stack, pop a certain number from stack....
HOw would I find the last word on a line?
-
Hi!
Read the whole line until the character '\n' is found.
-
Read the whole line from the file and then test (if-else statement) for a ' ' char. Understand?
--Garfield
-
Looking for the last word on the line:
begin by searching for a space OR a newline, read everything.
if a newline is hit, this is the last word.
if a space is hit, set your index variable (usually i) to 0 and set the
string you used for reading to '\0', like string[0] = '\0'
continue reading the line
repeat
the string you used should hold the last word of the line
by using getchar, you can easily read from input and only store the last word
else you'll have to use another index variable (j perhaps) and never set it to 0, unless the whole line is finished
for reading from file you'll need fgetc (of course)