how to find common suffix in java by using method

拥有回忆 提交于 2020-01-15 08:24:31

问题


How to find common suffix in java by using method

public static String commonSuffix (String s1, String s2)

I can't return the result in method. Please help me

import java.util.Scanner;

public class ders1 {

    public static void main(String[] args) {
        //HW3 Topic 3
        Scanner input = new Scanner(System.in);

        String reverse1="";
        String reverse2="";
     System.out.println("Please enter the first string: ");
     String s1=input.nextLine();

     System.out.println("Please enter the second string: ");
     String s2=input.nextLine();

     int l1=reverse1.length();
     int l2=reverse2.length();



     for ( int i = s1.length() - 1 ; i >= 0 ; i-- )
     {
        reverse1 = reverse1 + s1.charAt(i);
     }

     for ( int i = s2.length() - 1 ; i >= 0 ; i-- )
     {
        reverse2 = reverse2 + s2.charAt(i);
     }
     if(l1<l2){ 
        int l3=l2;

     System.out.println(reverse1 + " " + reverse2);
     System.out.println(commonSuffix(reverse1,reverse2,l3));
     }

     else {
         int l3=l1;
         System.out.println(reverse1 + " " + reverse2);

         System.out.println(commonSuffix(reverse1,reverse2,l3)); 
     }
    } 
    public static String commonSuffix (String reverse1, String reverse2,int l3){

        String suffixies="";
        for(int k=0; k<=l3 ; k++){
            if(reverse1.charAt(k)!=reverse2.charAt(k)){

        }
            else{
            suffixies+=reverse1.charAt(k);  
        }    
    }
        return suffixies;
   }
}       

Can someone help me fix this code??


回答1:


Please see the following code (I have tested it):

import java.util.Scanner;

public class ders1 {

    public static void main(String[] args) {
        // HW3 Topic 3
        Scanner input = new Scanner(System.in);

        String reverse1 = "";
        String reverse2 = "";
        System.out.println("Please enter the first string: ");
        String s1 = input.nextLine();

        System.out.println("Please enter the second string: ");
        String s2 = input.nextLine();

        for (int i = s1.length() - 1; i >= 0; i--) {
            reverse1 = reverse1 + s1.charAt(i);
        }

        for (int i = s2.length() - 1; i >= 0; i--) {
            reverse2 = reverse2 + s2.charAt(i);
        }

        int l1 = reverse1.length();
        int l2 = reverse2.length();

        if (l1 < l2) {
            int l3 = l1;
            System.out.println(reverse1 + " " + reverse2);
            System.out.println(commonSuffix(reverse1, reverse2, l3));
        } else {
            int l3 = l2;
            System.out.println(reverse1 + " " + reverse2);
            System.out.println(commonSuffix(reverse1, reverse2, l3));
        }
    }

    public static String commonSuffix(String reverse1, String reverse2, int l3) {
        String suffixies = "";
        for (int k = 0; k < l3; k++) {
            if (reverse1.charAt(k) != reverse2.charAt(k)) {
                break;
            } else {
                suffixies += reverse1.charAt(k);
            }
        }

        // Reverse again
        String reverse = "";
        for (int i = suffixies.length() - 1; i >= 0; i--) {
            reverse = reverse + suffixies.charAt(i);
        }

        return reverse;
    }
}

Output:

Please enter the first string: 
caption
Please enter the second string: 
action
noitpac noitca
tion



回答2:


Your issue is that you return inside of the for loop. You should return after the for loop terminates.




回答3:


This could be the best

** package com.tm;

import java.util.Scanner;

public class CommSuffix {

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    String str[] = scan.nextLine().split(",");

    String commSufffix=null;
    if(str[0].trim().length() > str[1].trim().length()) {
            String temp = str[0].trim();
            str[0] = str[1].trim();
            str[1] = temp;
    }

    for(int i=0;i<str[0].length();i++) {
        String subStr = str[0].substring(i,str[0].length());
        if(str[1].endsWith(subStr)) {
            commSufffix = subStr;
            break;
        }
    }

    System.out.println(commSufffix);
}

} **



来源:https://stackoverflow.com/questions/33839443/how-to-find-common-suffix-in-java-by-using-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!