Page 359 - Computer science 868 Class 12
P. 359

Some programs related to StringBuffer Class

                 Example 1:  Write a program in Java to accept a string and display the new string after reversing the characters of each
                 word.

                 Sample Input:
                 Understanding Computer Science

                 Sample output:

                 gnidnatsrednU retupmoC ecneicS
                 Solution

                    import java.util.*;
                    class reversestring
                    {
                        public static void main()
                        {
                            Scanner sc = new Scanner(System.in);
                            String str, revStr="";
                            int len;
                            System.out.println("Enter a string:");
                            str = sc.nextLine();
                            len = str.length();
                            StringBuffer ob= new StringBuffer(str);
                            StringTokenizer st = new StringTokenizer(str," ,.");
                            while (st.hasMoreTokens())
                            {
                                StringBuffer word = new StringBuffer(st.nextToken());
                                int wordLen = word.length();
                                revStr = revStr  + word.reverse() + " ";
                            }
                            revStr = revStr + ".";

                            System.out.println("String with words reversed:");
                            System.out.println(revStr);
                        }
                    }
                 Example 2: Caesar Cipher is an encryption technique which is implemented as ROT13 ('rotate by 13 places'). It is a
                 simple letter substitution cipher that replaces a letter with the letter 13 places after it in the alphabets, with the other
                 characters remaining unchanged.
                 ROT13

                        A/a    B/b     C/c    D/d    E/e    F/f    G/g    H/h     I/i    J/j    K/k     L/l   M/m
                         ↕      ↕      ↕      ↕       ↕      ↕      ↕      ↕      ↕       ↕      ↕      ↕      ↕
                        N/n    O/o     P/p    Q/q    R/r    S/s     T/t   U/u     V/v   W/w     X/x     Y/y    Z/z

                 Write a program to accept a plain text of length L, where L must be greater than 3 and less than 100.
                 Encrypt the text if valid as per the Caesar Cipher.






                                                                                                                       357
                                                                                                              Strings  357
   354   355   356   357   358   359   360   361   362   363   364