Click here to Skip to main content
15,887,875 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Java developers,
I am not so old in java and still on my learning period.
I was practicing java and came up with a problem.
The description is below.
Oliver and Lucy play game "Guess: which letter will be the last one?". Lucy names a guessed word and asks Oliver, which letter will be the last one. Rules of the games are the following:
(a) at first, remove every second letter, 
(b) then reverse the string of remaining letters (turn in backward order), 
(c) repeat steps a and b until single letter left. 
For example, when Lucy names word "KARTUPELIS", the wright answer is "I", because KARTUPELIS -> KRUEI -> IEURK ->  IUK -> KUI ->  KU -> IK ->  I. 
Write program that reads data from text file uzd9.in the given words; each word is written in separate line; maximum length of the word is 30 letters; word can include both uppercase, and lowercase Latin alphabet letters; the file contains no more than 100 words. The program must write the last remaind letter for each word after the gameplay in text file uzd9.out; each result in separate line. 
 
Example, Input data Wordsbefore.in KARTUPELIS BUMBA KURMIS SUNS
Output data wordsafter.out   I     A     K      N 
.
below is what i have tried. It kinda works, but, i have to convert the word to chararray everytime and then run a loop through it. It gives result but, to me it is not a good practice. I want to run a loop through those words and keep running until single letter is left in all words.
Please help me that, what can i do to run a cycle that, goes through the words, does it's job and checks if single letter is left. if not, then does the same thing and keeps doing until the single letter is left in each word.
"After running the code below, the output that i get is:"
"kui"
I have "kartupeli" in my txt file.

What I have tried:

package com.io.java;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class buffered {

	public static void main(String[] args)
	{
		buffered b = new buffered();
		String textFile = "words.txt";
		b.readFile(textFile);
		String newstr = "";
	}
    public void readFile(String strFile) 
	{
		try (BufferedReader buffer = new BufferedReader(new FileReader(strFile)))
			{
				String strBuffer;
				while((strBuffer = buffer.readLine())  != null && strBuffer.length()>1)
				{
					String n = "";
					String newtr = "";
				    int k;
			        char[] c = strBuffer.toCharArray();
				    for( k=0; k<=c.length;k++)
					{
						if(k%2==0)
						{
							newtr+=c[k];
						}
					}
			       newtr = reverse(newtr);
			       c=newtr.toCharArray();
                  if(c.length>0)
                {
                 for(int j = 0; j<=c.length;j++)
                   {
                	   if(j%2==0)
                	   {
                		 n+=c[j];   
                	   }
                   }
                }
                  else
                  {
                	  break;
                  }
                  n = reverse(n);
                   System.out.println(n);
				}
				buffer.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
	public void writeFile(String strFile, String data)
	{
		try(BufferedWriter bwriter = new BufferedWriter(new FileWriter(strFile, true)))
		{
			bwriter.write(data);
		} catch (IOException e) {
			
			e.printStackTrace();
		}
	}
	public String reverse(String s)
	{
		s = new StringBuffer(s).reverse().toString();
		return s;
	}
}
Posted
Updated 27-Mar-19 22:24pm
v2
Comments
Mohibur Rashid 28-Mar-19 2:48am    
Please define "every second"
Nabeel Munir 28-Mar-19 19:21pm    
It means that, a program should be coded in a way that it removes every 2nd(second) letter from the word. for example, let's suppose we have a word "Potato"
i will write a program that will remove every second letter from this word. like,
"oao" will be removed and "ptt" will be left and then u reverse the remaining letters. so, ptt will become, ttp and again remove second letter and revrse it. after removing every second letter from ttp we will have, tp and reverse again we will get, pt then again remove second letter and we will be left with p.
I want to do all these steps with a single loop.
So, i want to write a program that first removes every second word and reverse it and then checks if there is a single letter left, if not then go back to loop and remove every second word again checks if single letter is left and the cycle goes on until a single letter is left.
Nabeel Munir 28-Mar-19 19:27pm    
I don't need the whole code for this program. That i want to do by myself, i just need a suggestion on how can i implement such code in java that i defined. a cycle that keeps removing and reversing until a single letter is left.
I have tried many possible ways but, seems like my knowledge of java is not enough. I need to gain more knowledge.

1 solution

You could use, for instance, a double linked list (Java provides it, see LinkedList (Java Platform SE 8 )[^]).
Another possible approach is working with a character array, replacing with blanks the deleted characters.
 
Share this answer
 
Comments
Maciej Los 29-Mar-19 13:25pm    
5ed!
CPallini 30-Mar-19 6:59am    
Thank you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900