0
import java.io.*;
 
public class myFLAMES {
	public static void main(String args[]) throws Exception
	{
		DataInputStream input=new DataInputStream(System.in);
		int ctr=0;
		
		/* ask for inputs */
		System.out.println("Input your name: ");
		String name = input.readLine();
		
		System.out.println("Input her name: ");
		String sname = input.readLine();
		
		/* hard part -PROCESSING!!!! */
		
		/* we need to compare this two names */
		
		for(i=0;i<name.length();i++)
        {
         	for(j=0;j<sname.length();j++)
         	{
           		if(name.charAt(i)==sname.charAt(j))
            	{
             	ctr++;
            	}
         	}
		}
		
		System.out.println("a: "+ctr);
		
	}
}

I need help for counting the number of similarities between the two strings.

Example:
if I entered JUAN, then JUANA.

The answer should be : 5

Because JUAN has 5 characters with JUANA, those are "J","U","A","N","A"

I was able to do that with my code and was correct.

But how will i count "JUANA" similarity with "JUAN"?

The answer should be: 4

because JUANA has 4 characters in common with JUAN and those are "J","U","A","N".

how do I do this? thanks

2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by nschessnerd
0
import java.io.*;
 
public class myFLAMES {
    public static void main(String args[]) throws Exception
    {
        DataInputStream input=new DataInputStream(System.in);
        int ctr=0;
        
        /* ask for inputs */
        System.out.println("Input your name: ");
        String name = input.readLine();
        
        System.out.println("Input her name: ");
        String sname = input.readLine();
        
        /* hard part -PROCESSING!!!! */
        
        /* we need to compare this two names */
        
        for(i=0;i<name.length();i++)
        {
             for(j=0;j<sname.length();j++)
             {
                   if(name.charAt(i)==sname.charAt(j))
                {
                 ctr++;
j=sname.length();
                }
             }
        }
        
        System.out.println("a: "+ctr);
        
    }
}

basically once you find a match move to the next letter

also you may want to change the case of each string so it doesnt miss letters due to capitalization

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.