Java Programming Question A professor assigned a program to his class for which
ID: 655810 • Letter: J
Question
Java Programming Question
A professor assigned a program to his class for which the output is a string of lower-case letters. Unfortunately, he did not specify an ordering of the characters in the string, so he is having difficulty grading student submissions. Because of that, he has requested that ICPC teams help by writing a program that inputs pairs of strings of lower-case letters and determines whether or not the strings have the same letters, though possibly in different orders. Note that repeated letters are important; the string
Explanation / Answer
import java.util.*;
import java.io.*;
public class StringEquality
{
public static void main(String[] args)
{
String str1="",str2="";
int cnt=0,cnt1=0,cnt2=0;
System.out.println("Please enter pairs of string : ");
Scanner sc = new Scanner(System.in);
while(str1!="END" && str2!="END")
{
str1=sc.nextLine();
str2=sc.nextLine();
if(str1!="END" && str2!="END")
break;
if (str1.length() > 1000 || str2.length() > 1000)
{
System.out.println("Input line cannot be longer than 1000 characters!!!");
}
else
{
cnt++;
if (str1.length() != str2.length())
System.out.println("Case " + cnt + ": different ");
else
{
char[] ch1 = str1.toCharArray();
char[] ch2 = str2.toCharArray();
boolean equal=true;
for(int i=0;i<ch1.length;i++)
{
char ch = ch1[i];
for(int m=0;m<ch1.length;m++)
{
if (ch == ch1[m])
cnt1++;
}
for(int n=0;n<ch1.length;n++)
{
if (ch == ch2[n])
cnt2++;
}
if (cnt1!=cnt2)
{
System.out.println("Case " + cnt + ": different ");
equal=false;
break;
}
}
if(equal==true)
System.out.println("Case " + cnt + ": same ");
}
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.