package com.stringrelatedfunction;
public class StringTaskOnComparisson {
public static void main(String[] args) {
/* Reading two Strings as input */
String one = "Vikram is doing a task of which a programming on Strings";
String two = "Vikram is doing a task of which a programming is Arrays of jjjj";
/* Taking third string as empty String */
String three = " ";
/* Printing the length of both the strings */
System.out.println(one.length());
System.out.println(two.length());
/* Comparing two strings using equals method */
if (one.equals(two)) {
System.out.println("both are same");
}
/* Else checking length of String One is Greater than String two */
else {
if (one.length() > two.length()) {
/*
* If yes then appending String three which is empty string in
* String two such that both the string length becomes equal
*/
for (int i = 0; i < one.length() - 1; i++) {
if (one.charAt(i) != two.charAt(i)) {
two += three;
}
}
}
/* Similar to the above condition here if one is smaller than two */
else if (one.length() < two.length()) {
for (int i = 0; i < two.length() - 1; i++) {
if (two.charAt(i) != ' ') {
one += three;
}
}
}
System.out.println(one.length());
System.out.println(two.length());
/* Converting String into char array to compare each character */
char[] oneChar = one.toCharArray();
char[] twoChar = two.toCharArray();
System.out.println("non similar words are");
/*
* Comparing Strings character by character and printing the
* dis-similar ones
*/
if (one.length() > two.length()) {
System.out.print("{");
for (int i = 0; i <= one.length() - 1; i++) {
if (oneChar[i] != twoChar[i]) {
System.out.print(twoChar[i]);
}
}
System.out.println("}");
} else if (one.length() < two.length()) {
System.out.print("{");
for (int i = 0; i <= two.length() - 1; i++) {
if (twoChar[i] != oneChar[i]) {
System.out.print(oneChar[i]);
}
}
System.out.println("}");
}
}
}
}
我想打印输出为:Vikram正在执行一项任务,其中编程[在字符串] {是jjjj的数组}所以不相似的单词与相似的一次分开例如只是看到这个网站为它如何比较两个单词串:textdiff.com
答案 0 :(得分:0)
package com.stringrelatedfunction;
public class StringTaskOnComparisson {
private static String one;
private static String two;
public StringTaskOnComparisson(String one, String two) {
this.one = one;
this.two = two;
}
public static void comparingStrings() {
one = one.replaceAll("\\s{2}", "");
two = two.replaceAll("\\s{2}", "");
one = one.trim();
two = two.trim();
System.out.println(one);
System.out.println(two);
String[] temp1 = one.split(" ");
String[] temp2 = two.split(" ");
for (int i = 0; i < temp2.length; i++) {
if (temp1[i].compareTo(temp2[i]) == 0) {
System.out.print(" " + temp1[i]);
}
if (temp1[i].compareTo(temp2[i]) != 0) {
System.out.print("[" + temp1[i] + "]");
}
if (temp2[i].compareTo(temp1[i]) != 0) {
System.out.print("{" + temp2[i] + "}");
}
}
}
public static void main(String[] args) {
StringTaskOnComparisson stoc = new StringTaskOnComparisson("this is my first program"," vikram is my name ");
StringTaskOnComparisson.comparingStrings();
}
}
输出:
这是我的第一个项目 vikram是我的名字
[this] {vikram}是我的[first] {name}
答案 1 :(得分:0)
package com.stringrelatedfunction;
public class StringTaskOnComprassion2 {
private String s1;
private String s2;
public StringTaskOnComprassion2(String s1, String s2) {
this.s1 = s1;
this.s2 = s2;
}
public static void main(String[] args) {
String s1 = "Same Different same differentAgain inSquareBraces ";
String s2 = " Same different same DifferentAgain inFlowerBraces ";
StringTaskOnComprassion2 sc = new StringTaskOnComprassion2(s1, s2);
System.out.println(sc.compareString());
}
public String compareString() {
s1 = s1.replaceAll("\\s{2,}", " ").trim();
s2 = s2.replaceAll("\\s{2,}", " ").trim();
String str1[] = s1.split(" ");
String str2[] = s2.split(" ");
String temp = "";
int min = ((str1.length <= str2.length) ? (str1.length) : (str2.length));
int max = ((str1.length >= str2.length) ? (str1.length) : (str2.length));
for (int i = 0; i < max; i++) {
if (i < min) {
temp += (str1[i].equals(str2[i]) ? (str1[i] + " ") : ("[" + str1[i] + "]" + " " + "{" + str2[i] + "}"+" "));
} else if (max == str1.length) {
temp += ("[" + str1[i] + "]" + " ");
} else {
temp += ("{" + str2[i] + "}" + " ");
}
}
return temp;
}
}
/*output:
Same [Different] {different} same [differentAgain] {DifferentAgain} [inSquareBraces] {inFlowerBraces}
*/
答案 2 :(得分:0)
你应该有两个字符串输入的字符串列表,并比较两个列表中的每个字符串。如果有一个不匹配的元素,那么对该字符串元素执行任何操作。但是在将字符串输入到列表之前,首先将其过滤到char数组,以便只将单词添加到列表中,而不是标点符号和特殊字符。