কম্পিউটার

জাভাস্ক্রিপ্টে শব্দ না ভেঙে নির্দিষ্ট দৈর্ঘ্যের ব্লকে বাক্যকে কীভাবে বিভক্ত করবেন


আমাদের একটি জাভাস্ক্রিপ্ট ফাংশন লিখতে হবে যা একটি স্ট্রিং নেয় যাতে প্রথম আর্গুমেন্ট হিসাবে একটি অনুচ্ছেদের পাঠ্য এবং দ্বিতীয় আর্গুমেন্ট হিসাবে একটি খণ্ড আকারের সংখ্যা থাকে৷

ফাংশন নিম্নলিখিত জিনিসগুলি করা উচিত -

  • স্ট্রিংটিকে খণ্ড আকারের (দ্বিতীয় যুক্তি) থেকে বেশি নয় এমন দৈর্ঘ্যের খণ্ডে ভাঙ্গুন,

  • ব্রেকিং শুধুমাত্র হোয়াইটস্পেস বা বাক্যের শেষে ঘটতে হবে (শব্দ ভাঙা উচিত নয়)।

উদাহরণস্বরূপ − যদি ইনপুট স্ট্রিং হয় −

const str = 'this is a string';
const chunkLength = 6;

তারপর আউটপুট −

হওয়া উচিত
const output = ['this', 'is a', 'string'];

আসুন এই ফাংশনের জন্য কোড লিখি −

নির্দিষ্ট সংখ্যক অক্ষরের সাথে মেলাতে আমরা একটি রেগুলার এক্সপ্রেশন ব্যবহার করব। একবার মিলে গেলে আমরা ব্যাকট্র্যাক করব যতক্ষণ না আমরা একটি হোয়াইটস্পেস বা স্ট্রিংয়ের শেষ খুঁজে পাচ্ছি।

উদাহরণ

এর জন্য কোড হবে −

const size = 200;
const str = "This process was continued for several years for the deaf
child does not here in a month or even in two or three years the
numberless items and expressions using the simplest daily intercourse
little hearing child learns from these constant rotation and imitation the
conversation he hears in his home simulates is mine and suggest topics and
called forth the spontaneous expression of his own thoughts.";
const splitString = (str = '', size) > {
   const regex = new RegExp(String.raw`\S.{1,${size &minu; 2}}\S(?= |$)`,
   'g');
   const chunks = str.match(regex);
   return chunks;
}
console.log(splitString(str, size));

আউটপুট

এবং কনসোলে আউটপুট হবে −

[
   'This process was continued for several years for the deaf child does
   not here in a month or even in two or three years the numberless items and
   expressions using the simplest daily intercourse little',
   'hearing child learns from these constant rotation and imitation the
   conversation he hears in his home simulates is mine and suggest topics and
   called forth the spontaneous expression of his own',
   'thoughts.'
]

  1. জাভাস্ক্রিপ্টে ম্যাট্রিক্সে শব্দ খোঁজা

  2. জাভাস্ক্রিপ্টে একটি বাক্য থেকে n সবচেয়ে ঘন ঘন শব্দ খোঁজা

  3. জাভাস্ক্রিপ্টে ক্যামেলকেস সিনট্যাক্স ভাঙা

  4. বাক্যটিকে C++ এ শব্দে ভাগ করুন