I was asked this question in an interview, if an application is transmitting some text, for e.g a client makes an http call and in the body of post has a text message, then how does the server side know when it has received the complete message from start to end. In other words, if you wanted to count the number of words in this stream of text, how can you do that? Normal word counting is done through a state machine sort of approach, where you keep track of spaces and increment only once for a space between 2 words. Is counting words from a stream any different ? How ?
I was asked this question in an interview, if an application is transmitting some
Share
EOF character indicates the end of message and also the clients would generally flush and close the stream once they are done. That’s how the server would know that the transmission from the client has ended.
Irrespective of the source of text (whether it is coming from a String variable or from a stream), the logic to determine the number of words would stay the same, except that the input from string ends once the loop counter reaches the length of the string whereas in the stream, it is the EOF character that stops the loop.