Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Bangladesh University of Business &Technology

Lab Report: 01
Course Title:Compiler Design Lab
Course Code: CSE-324

Submitted By Submitted To
Name: Md. Saddam Hossain.
Name: Omar Farque Assistant Professor
Department of CSE BUBT
ID: 22234103118
Intake:50
Section:3
Department of CSE

Date:10-01-2024
Signature of the instructor

…………………………...
Problem no 01:
Split the words into meaningfull token based on different types of delimetery.
Sample input:
I love my contry $very; much.
Sample output:
I Love my contry very much.

Code:
#include <iostream>
#include <string>
#include <vector>

std::vector<std::string> tokenize(const std::string& sentence) {


std::vector<std::string> tokens;
std::string token;

for (char c : sentence) {


if (c == ' ') {
if (!token.empty()) {
tokens.push_back(token);
token.clear();
}
} else {
token.push_back(c);
}
}

if (!token.empty()) {
tokens.push_back(token);
}

return tokens;
}

int main() {
std::string sentence;
getline(std::cin, sentence);

std::vector<std::string> tokens = tokenize(sentence);

// Print tokens separated by space


for (const std::string& token : tokens) {
std::cout << token << " ";
}
std::cout << std::endl;

// Print tokens enclosed in angle brackets


for (const std::string& token : tokens) {
std::cout << "<" << token << ">" << std::endl;
}

return 0;
}
Output:

Conclusion: The C++ code effectively removes white spaces from a user-input sentence and
outputs the modified sentence along with the count of removed white spaces.

You might also like