my question is asking this: Write a program that compares two input strings. Output the number of characters that match in each string position. The output should use the correct verb (match vs matches) according to the character count.
Ex: If the input is: crush crash
the output is: 4 characters match
this is what i have so far:
import java.util.Scanner;
public class LabProgram
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
String str1 = in.next();
String str2 = in.next();
int counter=0;
if(str1.indexof(0)==str2.indexof(0)){
counter ;
System.out.println(counter "character match");
else
System.out.println("All characters match");
}
}
I know it doesn't look like a lot but I've tried this so many other ways but I clearly am missing something that would make this easier to do. I wanted to count the similar letters in my counter...but I don't know what to do.
CodePudding user response:
Use a for
loop to iterate across the string inputs and utilize charAt()
to look at specific characters within a string
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String str1 = in.next();
String str2 = in.next();
int counter=0;
// Loop across the smaller input string
// Math.min gives us the lesser of 2 values
for (int i = 0; i < Math.min(str1.length(), str2.length()); i ){
// If two characters match
if (str1.charAt(i) == str2.charAt(i)){
// Increment counter
counter ;
}
}
// If counter = length of both strings
if (counter == str1.length() && counter == str2.length()){
System.out.println("All characters match");
// If counter is 1
} else if (counter == 1){
System.out.println("1 character matches");
// Otherwise
} else {
System.out.println(counter " characters match");
}
}
Note: To use Math.Min()
, you'll need to
import java.lang.Math;
It's not completely necessary though; you could just figure out which string is longer on your own
CodePudding user response:
Code point
Avoid using char
. That type is essentially broken since Java 2, legacy since Java 5. As a 16-bit value, char
is physically incapable of representing most characters. One emoji could ruin your whole day.
Instead, use code point integer numbers when working with individual characters. You’ll find code point related methods on several classes, including String
, StringBuilder
, and Character
.
int[] codePointsX = x.codePoints().toArray() ;
int[] codePointsY = y.codePoints().toArray() ;
First, check lengths.
boolean sameLength = ( codePointsX.length == codePointsY.length ) ;
if ( ! sameLength ) { … }
Create an array to hold results of comparing each character.
int[] codePointsZ = new int[ codePointsX.length ] ;
Compare. The ternary operator ?:
is a condensed alternative to using an if
test.
We put a zero into results array if the two code points match. Else, we put a zero into results array. The default is a zero, so we could skip the assignment of zero. But I want our intentions to be crystal clear to the reader.
for( int index = 0 ; index < codePointsX.length ; index )
{
codePointsZ[ index ] =
codePointsX[ index ] == codePointsY[ index ] ? 1 : 0 ;
}
Results.
int countMatches = Arrays.stream( codePointsZ ).sum();