ساخت یک انکودر ساده توسط جاوا

ساخت یک انکودر ساده توسط جاوا :

می خواهیم برنامه ای بنویسیم که هر نوشته یا ( String ) را که به جاوا بدهیم آن را به بخش های سه حرفی جدا کند و سپس جای حرف اول و آخر را عوض کند و دوباره بخش های سه حرفی را به هم بچسپاند :

برای مثال ( RMR-Soft ) تبدیل شود به ( RMRoS-tf ). می دانیم که در این صورت اگر نوشته ی تغییر یافته را به جاوا بدهیم دوباره همان نوشته ی اصلی را به ما تحویل می دهد :

import javax.swing.*;

public class encoder

{

          public static void main (String [] args )

          {

                    String temp=JOptionPane.showInputDialog("Please enter the text");

                    int a=temp.length();

                    int b=a/3;

                    String temp2=new String();

                    String result="";

                    char temp3[]=new char[3];

                    char res[]=new char[3];

                    for (int i=0;i<b*3;i=i+3)

                    {

                                        temp2=temp.substring(i,i+3);

                                        temp3=temp2.toCharArray();

                                        for (int j=0;j<=2;j++)

                                        {

                                                  res[2-j]=temp3[j];

                                        }

                                        temp2=new String(res);

                                        result=result.concat(temp2);

                    }

                    if (a%3==1)

                    {

                              result=result.concat(temp.substring(a-1));

                    }

                    else if (a%3==2)

                    {

                              for (int f=0;f<=1;f++)

                              {

                                        result=result.concat(temp.substring(a-1-f,a-f));

                              }

                    }

                    JOptionPane.showMessageDialog(null,"resulting String is: "+ result);

                   

                   

          }

}