Combine two strings with common alphabets and without repetition.


/**
 *A class STRICOM contains two strings and creates a new string using the common characters
 *present in both the strings(without repetition)
 *Example     "chinese"    and    "checkers"
 *New String:   "ches"
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class STRICOM {
static String stA, stB;

STRICOM(String a, String b) {
stA = a;
stB = b;
}

int fnLen(String S) {
return S.length();
}

String fnCommon() {
String newSt = "";
int len2 = fnLen(stB);
for (int a = 0; a < stA.length(); a++) {
inner: for (int b = 0; b < len2; b++) {
char ch1 = stA.charAt(a);
char ch2 = stB.charAt(b);
if (ch1 == ch2) {
newSt += ch1;
break inner;
}
}
}
return newSt;
}

void fnShow() {
System.out.println("Inputted Strings\n" + stA + "\t\t" + stB);
String retVal = fnCommon();
System.out.println("\nCommon alphabets" + retVal);
}

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the two Strings");
String x = br.readLine();
String y = br.readLine();

STRICOM object = new STRICOM(x, y);
object.fnCommon();
object.fnShow();

br.close();
}
}

/**
 * Sample Output: Enter the two Strings chinese checkers Inputted Strings
 * chinese checkers
 *
 * chese
 */

Comments

Popular posts from this blog

Saddle Point

PSEUDO Series