基于reverse的java字符串

基于reverse的java字符串,java,string,Java,String,如何在input_1中反转整个段落(input_2中的单词除外)? 输出应该是这样的 String input_1="this is me.is this is me"; String input_2="is,me"; String output="me is siht is.me is siht"; 请参考以下示例:- public class ReverseWords { public static void main(String[] args) { St

如何在input_1中反转整个段落(input_2中的单词除外)? 输出应该是这样的

 String input_1="this is me.is this is me";
 String input_2="is,me";

 String output="me is siht is.me is siht";

请参考以下示例:-

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());
    }
}

要处理此类字符串处理示例,您必须了解有关正则表达式和字符串处理函数的更多信息。

虽然您的问题是关于输入表达式语法的,但我将在假设您输入第二个字符串的情况下回答您的问题(字符串输入2与代码片段中的一样)作为所有不可反转单词的列表,用逗号分隔。据我所知,这是您问题的工作代码解决方案。 请原谅我的缩进和格式,因为我写在维姆在一瞬间

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
  } 
}
import java.io.*;
公共类选择反向
{
公共静态void main(字符串args[])引发IOException
{
BufferedReader buf=新的BufferedReader(新的InputStreamReader(System.in));
System.out.println(“输入要选择性反转的字符串”);
字符串str=buf.readLine();
str=str.trim();
str=”“.concat(str);
int len=str.length();//第一个输入字符串的长度
System.out.println(“输入在反转过程中保持完整的文字”);
字符串s=buf.readLine();
int l=s.length();//第二个输入字符串的长度
int j=0;
int k=len-1;//存储提取到哪个子字符串的索引
int-wordcount=1;
String result=“;//存储输出字符串

对于(int i=0;i您尝试过什么吗?欢迎来到Stack Overflow!这里的用户通常更喜欢回答显示作者已经尝试过的内容的问题:)向我们展示您得到的东西!欢迎使用Stack Overflow!为了给您一个很好的答案,如果您还没有浏览一下,它可能会对我们有所帮助。如果您可以提供。