rakesh830566

String Anagram

May 28th, 2020
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 1.93 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.  
  4. <head>
  5.     <meta charset="UTF-8">
  6.     <title>Assignment 4.12</title>
  7. </head>
  8.  
  9. <body>
  10.     <h3>Write a program of String Anagram</h3>
  11.     <hr>
  12.     <script type="text/javascript">
  13.         // Take input two strings from user
  14.         var userFirstInput = prompt('Enter a First String...');
  15.         var userSecondInput = prompt('Enter a Second String...');
  16.  
  17.         // Displaying strings which is entered by user
  18.         document.write('Entered string by you = <strong>' + userFirstInput + '</strong><br>');
  19.         document.write('Entered string by you = <strong>' + userSecondInput + '</strong><br><br>');
  20.  
  21.         // Check whether given strings are Anagram or not
  22.         var firstSplittedArray = userFirstInput.toLowerCase().split('').sort();
  23.         var secondSplittedArray = userSecondInput.toLowerCase().split('').sort();
  24.  
  25.         var notAlphabetRegEx = /[^a-z]+/;
  26.  
  27.         while (notAlphabetRegEx.test(firstSplittedArray[0])) {
  28.             firstSplittedArray.splice(0, 1);
  29.         }
  30.         while (notAlphabetRegEx.test(secondSplittedArray[0])) {
  31.             secondSplittedArray.splice(0, 1);
  32.         }
  33.  
  34.         var bool = false;
  35.         if (firstSplittedArray.length === secondSplittedArray.length) {
  36.             for (i in firstSplittedArray) {
  37.                 bool = firstSplittedArray[i] === secondSplittedArray[i];
  38.                 if (bool === false) {
  39.                     break;
  40.                 }
  41.             }
  42.         }
  43.  
  44.         // Displaying strings are Anagram or not
  45.         if (bool) {
  46.             document.write('String <strong>' + userFirstInput + '</strong> and String <strong>' + userSecondInput + '</strong>' + ' are Anagram of each and other.');
  47.         } else {
  48.             document.write('String <strong>' + userFirstInput + '</strong> and String <strong>' + userSecondInput + '</strong>' + ' are not Anagram of each and other.');
  49.         }
  50.     </script>
  51. </body>
  52.  
  53. </html>
Add Comment
Please, Sign In to add comment