String Output Alignment in Java -


practice question part 1

practice question part 2

this practice question rather hard me. below code static method(the main method fixed -unchangeable, , signature of static method given), , intention matches between characters , print them out.

but there concerns:

1) how ensure doesn't print when strings aligned there characters makes boolean false , result not aligned instead? (e.g amgk second string & first string java programming course)

2) how make print right? spaces off , letters aren't wanted.

3) if there more 1 character in str1, choose put, , how omit rest when there match?

would appreciate pseudocode guide beginner me in solving problem.

public class q3 { public static void main(string[] args) {     scanner sc = new scanner(system.in);     system.out.print("enter first string:");     string input1 = sc.nextline();     system.out.print("enter second string:");     string input2 = sc.nextline();     system.out.println();      if (matchstrings(input1, input2)) {         system.out.println();         system.out.println("there alignment shown above.");     } else {         system.out.println("no alignment can found.");     } }     public static boolean matchstrings(string str1, string str2) {     // modify code below return correct value.     boolean ismatch = false;     //int firstchar = str2.charat(0);     //int lastchar = str2.charat(str2.length()-1);     int previndex = 0;      system.out.println(str1);      (int j = 0; j< str2.length(); j++) {         (int = 0; i<str1.length();i++) {             char chartosearch = str1.charat(i);             int newindex = i;              if (str2.charat(j)== chartosearch) {                 (int k = previndex; k < newindex-1; k++) {                     system.out.print(" ");                 }                 system.out.print(chartosearch);                 //previndex=newindex+1;                   ismatch = true;             }         }     }     return ismatch; } } 

i think 2 of first few structures learn in data structures course stack , queue. thus, provide implementation using stack. can use stack store test string , pop each char element off stack when matched character in first string. otherwise output empty space " " in matched string object:

    stack s2 = new stack();     string str1 = "java programming";     string str2 = "amg";      for(int = str2.length()-1; >= 0; i--){ //need populate stack backwards...lifo         s2.push(str2.charat(i));     }      string match = ""; //used store matching line      for(int = 0; < str1.length(); i++){         if(str1.charat(i) == (char)s2.peek()){             match += s2.pop().tostring();         }         else         {             match += " ";         }     }      system.out.println(str1);     system.out.println(match); 

you can use queue this, leave learn on own. practice on creating own stack object using arrays , integer pointers handle overflow/underflow.

the above code print out:

tested char comparison


Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -