BIS601 Program 2

PROGRAM:

const readline = require('readline');

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

function isPalindrome(str) {
  const reversedStr = str.split('').reverse().join('');
  return str === reversedStr;
}

rl.question('Enter a string: ', (inputString) => {
  const lengthOfString = inputString.length;
  console.log(`Length of the string: ${lengthOfString}`);

  const start = inputString.indexOf("JavaScript");
  if (start !== -1) {
    const extractedWord = inputString.slice(start, start + "JavaScript".length);
    console.log(`Extracted word: ${extractedWord}`);
  } else {
    console.log("Word 'JavaScript' not found in the string.");
  }

  const replacedString = inputString.replace('JavaScript', 'JavaScript lab program');
  console.log(`New string after replacement: ${replacedString}`);

  const isStringPalindrome = isPalindrome(inputString);
  console.log(`Is the string a palindrome? ${isStringPalindrome}`);

  rl.close();
});

Step 4: To run program use below command:

  • Open the vscode integrated terminal through the menu by selecting and type the below command to run the program.
node index.js

OUTPUT:

********************************output 1********************************

Enter a string: I am learning JavaScript from vtucircle website
Length of the string: 47
Extracted word: JavaScript
New string after replacement: I am learning JavaScript lab program from vtucircle website
Is the string a palindrome? false


********************************output 2********************************

Enter a string: madam
Length of the string: 5
Word 'JavaScript' not found in the string.
New string after replacement: madam
Is the string a palindrome? true
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x