循环通过字符串并将它们反转直到剩下1个字母 [英] Looping through strings and reversing them until 1 letter is left

查看:111
本文介绍了循环通过字符串并将它们反转直到剩下1个字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Java开发人员,

我在java中并不是那么老,而且还在我的学习期间。

我正在练java并想出了一个问题。 />
描述如下。

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 



以下是我所拥有的试过。它有点工作,但是,我必须每次将这个词转换为chararray,然后通过它循环。它给出了结果,但对我来说这不是一个好习惯。我想通过这些单词运行一个循环并继续运行,直到单个字母留在所有单词中。

请帮助我,我该怎么做才能运行一个循环,通过单词,确实这是工作,并检查是否留下单个字母。如果没有,那么做同样的事情并继续做,直到每个单词留下单个字母。

运行下面的代码后,我得到的输出是:

kui

我在我的txt文件中有 kartupeli



我尝试了什么:



.
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;
	}
}

推荐答案

例如,您可以使用双链表( Java 提供它,请参阅 LinkedList(Java平台) SE 8) [ ^ ])。

另一种可能的方法是使用字符数组,用空格替换已删除的字符。
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.


这篇关于循环通过字符串并将它们反转直到剩下1个字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆