Stringmanipulations

 

In another project, we deal with strings. String methods such as length (); charAt (int); toUpperCase (); toLowerCase (); indexOf (String); replace (char, char); substring (int, int); be applied here.

Excerpts from the source code:

public String code(String pText, int pOffset){
        String lText="";
        for (int i=0;i<pText.length();i++){
            char c = pText.charAt(i);
            lText+=code(c,pOffset);
        }
        return lText;
    }
    
    public char code(char pChar, int pOffset){
        String lAlphabet="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        if (pOffset<0||pOffset>25)pOffset=0;
        if (pChar==' ')return pChar;
        int lIndex = lAlphabet.indexOf(""+pChar);
        if (lIndex+pOffset<26)return lAlphabet.charAt(lIndex+pOffset);
        return lAlphabet.charAt(lIndex+pOffset-26);
    }
    
    public String decode(String pText, int pOffset){
        String lText="";
        if (pOffset<0||pOffset>25)pOffset=0;
        for (int i=0;i<pText.length();i++){
            char c = pText.charAt(i);
            lText+=letter(c,pOffset);
        }
        return lText;
    }
    
    public char letter(char pChar, int pOffset){
        String lAlphabet="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        if (pOffset<0||pOffset>25)pOffset=0;
        if (pChar==' ')return pChar;
        int lIndex = lAlphabet.indexOf(""+pChar);
        if (lIndex-pOffset>=0)return lAlphabet.charAt(lIndex-pOffset);
        return lAlphabet.charAt(lIndex-pOffset+26);
    }
    
    public int frequencyOfChars(String pText,char pChar){
        int lCount=0;
        for (int i=0;i<pText.length();i++){
            char lChar = pText.charAt(i);
            if (lChar==pChar)lCount++;
        }
        return lCount;
    }
    
    public boolean palindrome(String pText){
        boolean lPalindrome=true;
        pText=pText.toUpperCase();
        int i=0;
        while (lPalindrome&&i<pText.length()/2){
            lPalindrome=lPalindrome&&pText.charAt(i)==pText.charAt(pText.length()-i-1);
            i++;
        }
        return lPalindrome;
    }
    
    public String chineseLanguage(String pText){
        String lText="";
        for (int i=0;i<pText.length();i++){
            if (pText.charAt(i)=='R')lText+="L";
            else if (pText.charAt(i)=='r')lText+="l";
            else lText+=pText.charAt(i);
        }
        return lText;
    }