Odd word problem: Difference between revisions

From Rosetta Code
Content added Content deleted
m (typo)
(+Java)
Line 13: Line 13:


'''Test case''': work on both the "life" example given above, and the text <code>we,are;not,in,kansas;any,more.</code>
'''Test case''': work on both the "life" example given above, and the text <code>we,are;not,in,kansas;any,more.</code>
=={{header|Java}}==
This is translated from the first [[C]] version on the [http://c2.com/cgi/wiki?OddWordProblemSolutions solutions page].
<lang java>import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;

public class OddWord {
public static void processStream(BufferedReader in) throws IOException{
if(checkEnd(in))return;
while(true){
forward(in);
if(checkEnd(in))return;
reverse(in);
if(checkEnd(in))return;
}
}
private static boolean checkEnd(BufferedReader in) throws IOException{
if(peek(in) == '.'){
System.out.println((char)in.read());
return true;
}else{
System.out.print((char)in.read());
return false;
}
}
private static char peek(BufferedReader in) throws IOException{
in.mark(1);
char retVal = (char)in.read();
in.reset();
return retVal;
}
private static void forward(BufferedReader in) throws IOException{
while(Character.isLetter(peek(in))){
System.out.print((char)in.read());
}
}
private static void reverse(BufferedReader in) throws IOException{
if(Character.isLetter(peek(in))){
char character = (char)in.read();
reverse(in);
System.out.print(character);
}
}
public static void main(String[] args) throws IOException{
processStream(new BufferedReader(new StringReader("what,is,the;meaning,of:life.")));
processStream(new BufferedReader(new StringReader("we,are;not,in,kansas;any,more.")));
processStream(new BufferedReader(new StringReader(";what,is,the;meaning,of:life.")));
processStream(new BufferedReader(new StringReader("'we,are;not,in,kansas;any,more.")));
}
}</lang>
Output:
<pre>what,si,the;gninaem,of:efil.
we,era;not,ni,kansas;yna,more.
;what,si,the;gninaem,of:efil.
'we,era;not,ni,kansas;yna,more.</pre>

Revision as of 19:29, 3 November 2011

Odd word problem is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Write a program that solves the odd word problem.

Description: You are promised an input stream consisting of English letters and punctuations. It is guaranteed that

  • the words (sequence of consecutive letters) are delimited by one and only one punctuation; that
  • the words will be at least one letter long; and that
  • a full stop (.) appears after, and only after, the last word.

For example, what,is,the;meaning,of:life. is such a stream with six words. Your task is to reverse the letters in every other word while leaving punctuations intact, producing e.g. "what,si,the;gninaem,of:efil.", while observing the following restrictions:

  1. Only I/O allowed is reading or writing one character at a time;
  2. You are not to explicitly save characters in a collection data structure, such as arrays, strings, hash tables, etc, for later reversal;
  3. You are allowed to use recursions, closures, continuations, threads, coroutinnes, etc.

Test case: work on both the "life" example given above, and the text we,are;not,in,kansas;any,more.

Java

This is translated from the first C version on the solutions page. <lang java>import java.io.BufferedReader; import java.io.IOException; import java.io.StringReader;

public class OddWord { public static void processStream(BufferedReader in) throws IOException{ if(checkEnd(in))return; while(true){ forward(in); if(checkEnd(in))return; reverse(in); if(checkEnd(in))return; } }

private static boolean checkEnd(BufferedReader in) throws IOException{ if(peek(in) == '.'){ System.out.println((char)in.read()); return true; }else{ System.out.print((char)in.read()); return false; } }

private static char peek(BufferedReader in) throws IOException{ in.mark(1); char retVal = (char)in.read(); in.reset(); return retVal; }

private static void forward(BufferedReader in) throws IOException{ while(Character.isLetter(peek(in))){ System.out.print((char)in.read()); } }

private static void reverse(BufferedReader in) throws IOException{ if(Character.isLetter(peek(in))){ char character = (char)in.read(); reverse(in); System.out.print(character); } }

public static void main(String[] args) throws IOException{ processStream(new BufferedReader(new StringReader("what,is,the;meaning,of:life."))); processStream(new BufferedReader(new StringReader("we,are;not,in,kansas;any,more."))); processStream(new BufferedReader(new StringReader(";what,is,the;meaning,of:life."))); processStream(new BufferedReader(new StringReader("'we,are;not,in,kansas;any,more."))); } }</lang> Output:

what,si,the;gninaem,of:efil.
we,era;not,ni,kansas;yna,more.
;what,si,the;gninaem,of:efil.
'we,era;not,ni,kansas;yna,more.