কম্পিউটার টিউটোরিয়াল

C++-তে সেন্টেন্সকে ওয়ার্ডে ভাগ করা


দেওয়া হয়েছে একটি সেন্টেন্সকে ওয়ার্ডে ভাগ করার টাস্ক। এখানে আমরা সেন্টেন্সে থাকা সব ওয়ার্ড আলাদা করব।

ইনপুট

I am a good boy

আউটপুট

I
am
a
good
boy

উপরের উদাহরণে প্রতিটি ওয়ার্ডকে আলাদা লাইনে প্রিন্ট করা হয়েছে।

উদাহরণ ১

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

void split(string str) {
    stringstream ss(str);
    string word;
    while (ss >> word) {
        cout << word << endl;
    }
}

int main() {
    string st = "Blood from heaven is mortal";
    cout << "Blood from heaven is mortal" << endl;
    split(st);
    return 0;
}

আউটপুট

Blood from heaven is mortal
Blood
from
heaven
is
mortal

উদাহরণ ২

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

void split(string str) {
    stringstream ss(str);
    string word;
    while (ss >> word) {
        cout << word << endl;
    }
}

int main() {
    string st = "type a text message";
    cout << "type a text message" << endl;
    split(st);
    return 0;
}

আউটপুট

type a text message
type
a
text
message
  1. C++-এ দশমিক সংখ্যা শব্দে রূপান্তর করার প্রোগ্রাম (রেঞ্জ-ভিত্তিক)

  2. C++-এ বাক্যে প্যালিনড্রোম শব্দ সংখ্যা গণনা

  3. C++-এ The Maze II সমস্যা: গোলাপταν গেঁদের ন্যূনতম দূরত্ব

  4. C++-এ মেজ সমস্যা: বল দিয়ে পথ খুঁজে বের করা