从另一个字符串中出现的字符串打印字符

时间:2014-09-05 08:41:12

标签: java string

我希望打印出另一个字符串中的字符串中的字符;不会出现的字符替换为星号。

例如:

string 1: helloworld
string 2: hord

输出应为:

h***o**r*d

仅打印出字符串1中出现的字符串2中的字符;字符串1中未出现在字符串2中的字符表示为星号。

我写了以下代码:

public class ProgramonStrings {

    public static void main(String[] args) {
    {
        String str1 = "helloworld";
        String str2 = "hord";
        StringBuffer sbf = new StringBuffer();
        StringBuffer sbf2 = new StringBuffer();
        String output;
        for (int i = 0; i < str1.length(); i++) {
            int count = 0;
            for (int j = 0; j < str2.length(); j++) {
                if (str1.charAt(i) == str2.charAt(j)) {
                    sbf.append(str1.charAt(i));
                    count++;
                }
            }
            if (count == 0) {
                sbf.append('*');
            }
        }
        output = sbf.toString();
        for (int i = 0; i < output.length(); i++) {
            int count = 0;
            if (output.charAt(i) != '*') {
                for (int j = i + 1; j < output.length(); j++) {
                    if (output.charAt(j) == output.charAt(i)) {
                        //output.replace(output.charAt(j), '*');
                        sbf2.append('*');
                        count++;
                    }
                }
            }
            if (count == 0) {
                sbf2.append(output.charAt(i));
            }
        }
        System.out.println(sbf2);
        System.out.println(sbf2);
    }    
}

从那时起,我得到的输出为:h*****or*d

所以任何人都可以更正我的程序以获得适当的输出,例如:h***o**r*d

2 个答案:

答案 0 :(得分:2)

您只需要在文本字符串上迭代一次。迭代它并将字符串中的当前字符与第二个字符串中的下一个字符进行比较。如果它相等,则使用第二个字符串中的字符,即递增pointer。否则打印星号(*)。

如果pointer < str2.length()不是true,则表示第二个字符串中的所有字符都被使用。

public static void main(String[] args) {

    String str1 = "helloworld";
    String str2 = "hord";
    int pointer = 0;

    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < str1.length(); i++) {
        if (pointer < str2.length() && str1.charAt(i) == str2.charAt(pointer)) {
            sb.append(str1.charAt(i));
            pointer++;
        }
        else {
            sb.append('*');
        }
    }
    System.out.println(sb.toString());
}

请注意,最好使用StringBuilder代替StringBuffer

答案 1 :(得分:0)

您可以使用与此类似的代码来提供所需的输出

public class ProgramOnStrings {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Compare cobj=new Compare();
    cobj.compareStrings();
}

}

class Compare
{
String s1="helloworld";
String s2="hord";
int array[];
String small,big;

Compare()
{
    if(s1.length()<s2.length())
      {

          small=s1;
          big=s2;
          array=new int[small.length()];

      }
      else
      {
        small=s2;
        big=s1;
        array=new int[s2.length()];  

      }
    System.out.println("small\t"+small+"\tbig\t"+big);
}

public void compareStrings()
{
    int j=0;
   for(int i=0;i<big.length();i++)
  {

      if(small.charAt(j)==big.charAt(i))
      {
         array[j]=i;
         System.out.println("if: The selected positions are"+i);
        j++;  
      }


  }

  int m=0;
  for(int k=0;k<big.length();k++)
  {
      if(array[m]==k)
      {
          System.out.print(small.charAt(m));
          m++;
      }
      else
      {
      System.out.print("*");
      }
 }
}

}