0% found this document useful (0 votes)
32 views6 pages

Code (Solve)

Uploaded by

tazfiasultana67
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views6 pages

Code (Solve)

Uploaded by

tazfiasultana67
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

1....

#include <iostream>
#include <fstream>
#include <string>

int main() {
std::ifstream file("input.txt"); // Open the file for reading
if (!file.is_open()) { // Check if the file opened successfully
std::cerr << "Error: Could not open the file!" << std::endl;
return 1;
}

std::string line;
int lineNumber = 1;

while (std::getline(file, line)) { // Read the file line by line


std::cout << lineNumber << ". " << line << std::endl; // Print the line
with its number
lineNumber++; // Increment the line number
}

file.close(); // Close the file


return 0;
}

2......

#include <iostream>
#include <fstream>
#include <string>

int main() {
std::ifstream file("input.txt"); // Open the file for reading
if (!file.is_open()) { // Check if the file opened successfully
std::cerr << "Error: Could not open the file!" << std::endl;
return 1;
}

std::string line;
int lineCount = 0;

// Read each line from the file and increment the counter
while (std::getline(file, line)) {
lineCount++;
}

file.close(); // Close the file

// Print the number of lines


std::cout << "Number of lines in the file: " << lineCount << std::endl;

return 0;
}
3............

#include <iostream>
#include <fstream>
#include <string>

bool removeSingleLineComment(std::string &line) {


size_t commentPos = line.find("//");
if (commentPos != std::string::npos) {
line.erase(commentPos);
return true;
}
return false;
}

bool removeMultiLineComment(std::string &line, std::ifstream &file) {


size_t startCommentPos = line.find("/*");
size_t endCommentPos;

if (startCommentPos != std::string::npos) {
endCommentPos = line.find("*/", startCommentPos + 2);

// If the closing part of the multi-line comment is in the same line


if (endCommentPos != std::string::npos) {
line.erase(startCommentPos, endCommentPos - startCommentPos + 2);
} else {
// Remove everything after the start of the comment
line.erase(startCommentPos);

// Now keep reading lines until we find the closing '*/'


std::string nextLine;
while (std::getline(file, nextLine)) {
endCommentPos = nextLine.find("*/");
if (endCommentPos != std::string::npos) {
// Found closing comment part, remove it
line += nextLine.substr(endCommentPos + 2);
break;
}
}
}
return true;
}
return false;
}

int main() {
std::ifstream inputFile("input.txt"); // Open input file
std::ofstream outputFile("output.txt"); // Open output file

if (!inputFile.is_open() || !outputFile.is_open()) {
std::cerr << "Error: Could not open the file!" << std::endl;
return 1;
}

std::string line;
int deletionCount = 0;

// Read each line from the input file


while (std::getline(inputFile, line)) {
bool deleted = false;

// Remove single-line comments


if (removeSingleLineComment(line)) {
deletionCount++;
deleted = true;
}

// Remove multi-line comments


if (removeMultiLineComment(line, inputFile)) {
deletionCount++;
deleted = true;
}

// If comments were deleted, output the modified line


if (!line.empty() || !deleted) {
outputFile << line << std::endl;
}
}

inputFile.close();
outputFile.close();

// Print the number of deletions


std::cout << "Number of comments deleted: " << deletionCount << std::endl;

return 0;
}

4............

#include <iostream>
#include <string>

bool isSingleLineComment(const std::string &line) {


// Check if the line starts with '//' after ignoring leading whitespaces
size_t pos = line.find_first_not_of(" \t");
if (pos != std::string::npos && line.substr(pos, 2) == "//") {
return true;
}
return false;
}

bool isMultiLineCommentStart(const std::string &line) {


// Check if the line contains '/*' after ignoring leading whitespaces
size_t pos = line.find_first_not_of(" \t");
if (pos != std::string::npos && line.substr(pos, 2) == "/*") {
return true;
}
return false;
}

bool isMultiLineCommentEnd(const std::string &line) {


// Check if the line contains '*/'
if (line.find("*/") != std::string::npos) {
return true;
}
return false;
}

int main() {
std::string line;
bool insideMultiLineComment = false;

// Get the input line from the user


std::cout << "Enter a line of text: ";
std::getline(std::cin, line);

// Check if the line is a single-line comment


if (isSingleLineComment(line)) {
std::cout << "This is a single-line comment." << std::endl;
}
// Check if the line is the start of a multi-line comment
else if (isMultiLineCommentStart(line)) {
std::cout << "This is the start of a multi-line comment." << std::endl;
insideMultiLineComment = true;
}
// Check if the line is the end of a multi-line comment
else if (insideMultiLineComment && isMultiLineCommentEnd(line)) {
std::cout << "This is the end of a multi-line comment." << std::endl;
insideMultiLineComment = false;
}
else if (insideMultiLineComment) {
std::cout << "This line is inside a multi-line comment." << std::endl;
}
// If none of the above, it's not a comment
else {
std::cout << "This is not a comment." << std::endl;
}

return 0;
}

5............

#include <iostream>
#include <fstream>
#include <string>

int main() {
std::ifstream inputFile("input.txt"); // Open the input file
std::ofstream outputFile("output.txt"); // Open the output file

if (!inputFile.is_open() || !outputFile.is_open()) {
std::cerr << "Error: Could not open the file!" << std::endl;
return 1;
}

char ch;
int deletionCount = 0;

// Read character by character from the input file


while (inputFile.get(ch)) {
// Check if the character is a space, tab, or newline
if (ch == ' ' || ch == '\t' || ch == '\n') {
deletionCount++; // Increment deletion counter
} else {
// Write the character to the output file if it's not a space, tab, or
newline
outputFile.put(ch);
}
}

inputFile.close();
outputFile.close();

// Print the number of deletions


std::cout << "Number of tabs, spaces, and newlines deleted: " << deletionCount
<< std::endl;

return 0;
}

6.........

#include <iostream>
#include <cctype>
#include <set>
#include <string>

// Function to check if a given string is a valid identifier


bool isValidIdentifier(const std::string &identifier) {
// List of C/C++ keywords (incomplete list for simplicity)
const std::set<std::string> keywords = {
"auto", "break", "case", "char", "const", "continue", "default", "do",
"double",
"else", "enum", "extern", "float", "for", "goto", "if", "int", "long",
"register",
"return", "short", "signed", "sizeof", "static", "struct", "switch",
"typedef", "union", "unsigned", "void", "volatile", "while"
};

// Check if identifier is empty


if (identifier.empty()) {
return false;
}

// Check if the identifier is a keyword


if (keywords.find(identifier) != keywords.end()) {
return false;
}

// Check if the first character is a letter or underscore


if (!std::isalpha(identifier[0]) && identifier[0] != '_') {
return false;
}

// Check the rest of the characters (they must be letters, digits, or


underscores)
for (size_t i = 1; i < identifier.size(); ++i) {
if (!std::isalnum(identifier[i]) && identifier[i] != '_') {
return false;
}
}

// If all checks pass, the identifier is valid


return true;
}

int main() {
std::string identifier;

// Input an identifier from the user


std::cout << "Enter an identifier: ";
std::cin >> identifier;

// Check if the identifier is valid


if (isValidIdentifier(identifier)) {
std::cout << "\"" << identifier << "\" is a valid identifier." <<
std::endl;
} else {
std::cout << "\"" << identifier << "\" is not a valid identifier." <<
std::endl;
}

return 0;
}

You might also like