除了input_2中的单词外,如何反转input_1中的整个段落? 输出应该是这样的....
String input_1="this is me.is this is me";
String input_2="is,me";
String output="me is siht is.me is siht";
答案 0 :(得分:0)
使用以下示例作为参考: -
public class ReverseWords {
public static void main(String[] args) {
String input1 = "this is me.is this is me";
String input2 = "is,me";
String output = "";
String[] wordsFromInput1 = input1.split("[\\s.]");
String[] wordsFromInput2 = input2.split(",");
for (String tempWord1 : wordsFromInput1) {
boolean existInSecond = false;
for (String tempWord2 : wordsFromInput2) {
if (tempWord1.equals(tempWord2)) {
existInSecond = true;
break;
}
}
if (existInSecond) {
output += tempWord1 + " ";
} else {
output += new StringBuffer(tempWord1).reverse() + " ";
}
}
System.out.println(output.trim());
}
}
您必须详细了解正则表达式和字符串处理函数才能处理此类字符串处理示例。
答案 1 :(得分:0)
关于输入表达式语法的问题很模糊,我会回答它,假设你输入第二个字符串(字符串input_2,如你的代码片段中)作为所有不可逆转的单词列表,用逗号分隔。正如我所理解的那样,这是您问题的工作代码解决方案。 请原谅我的缩进和格式,就像我在VIM上写的一样。
import java.io.*;
public class SelectReverse
{
public static void main(String args[]) throws IOException
{
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the string to be selectively reversed");
String str = buf.readLine();
str = str.trim();
str = " ".concat(str);
int len = str.length(); //length of first input string
System.out.println("Enter the words to be left intact during reversal");
String s = buf.readLine();
int l = s.length(); //length of second input string
int j = 0;
int k = len-1; //stores index upto which substring is extracted
int wordcount = 1;
String result = ""; //stores output string
for(int i = 0; i<l; i++) //counts no. of words which are not to be reversed
{
if(s.charAt(i) == ',')
{
wordcount++;
}
}
String arr[] = new String[wordcount]; //array of words not to be reversed
int counter = 0;
for(int i = 0; i<l; i++)
{
if(s.charAt(i) == ',')
{
String subs = s.substring(j, i); //extracting individual words from list of words not to be reversed
arr[counter] = subs; //adding them in the array
j = i+1;
counter++;
}
}
arr[counter] = s.substring(j); //adding last word (after the last comma)
boolean firstflag = false;
for(int i = len-1; i>=0; i--)
{
String substr;
if(str.charAt(i)==' ' || str.charAt(i)=='.')
{
if(firstflag == false) //substring is extracted till end of string only for the first extracted word
{
substr = str.substring(i+1);
firstflag = true;
k = i;
}
else
{
substr = str.substring(i+1, k);
k = i;
}
boolean flag = false;
for(int m = 0; m<wordcount; m++)
{
if(arr[m].equalsIgnoreCase(substr))//true if substring is not to be reversed, i.e. matches with any word in array
{
flag = true;
}
}
if(flag == true) //concatenating substring to output string without reversing
{
result = result+substr;
result = result+" ";
}
else //concatenating substring to output string after reversing
{
String reverse = "";
int ln = substr.length();
for(int n = ln-1; n>=0; n--) //reversing substring
{
char ch = substr.charAt(n);
String chstring = Character.toString(ch);
reverse = reverse+chstring;
}
result = result+reverse;
result = result+" ";
}
}
}
System.out.println(result); //displaying resultant output string
}
}
这应该可以解决您的问题。有关更多问题,请更具体地说明输入语法,正则表达式和问题陈述。 您还可以使用.split(String,delimiter)方法在由分隔符分隔的String中创建子字符串数组,但为了清晰和基本级别的理解,我已经显示了该方法的实现。