0% found this document useful (0 votes)
25 views4 pages

C++ Lab Assignment

The document contains a lab assignment for a C++ course at VIT Bhopal University, focusing on strings and friend functions. It includes two programming tasks: one to check if two names are anagrams and another to convert a decimal number to binary using a class and a friend function. Sample code and input/output examples are provided for both tasks.

Uploaded by

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

C++ Lab Assignment

The document contains a lab assignment for a C++ course at VIT Bhopal University, focusing on strings and friend functions. It includes two programming tasks: one to check if two names are anagrams and another to convert a decimal number to binary using a class and a friend function. Sample code and input/output examples are provided for both tasks.

Uploaded by

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

SCHOOL OF COMPUTING SCIENCE AND ENGINEERING VIT BHOPAL UNIVERSITY,

BHOPAL
Object Oriented Programming using C++(CSE2001)
Session: Winter 2023-24
Lab Assignment -Strings & Friend Functions in C++

Submitted by- Arsh Mevati 23MIP10040

1. Write a program to check if the first and last name of a student is an anagram of each other or
not.
An anagram of a string is another string that contains the same characters, only the order of
characters can be different. For example, “act” and “tac” are anagram of each other. Invest your
time to study the locked code and complete it accordingly. Input:
The first line contains the number of test cases, each test case consists of one line input
containing 2 space separated strings representing the first and last name of the student.
Output:
For each test case T, the output should consist of a single line - ANAGRAM or NOT ANAGRAM
depending upon if the 2 given strings are anagram of each other or not.
Constraints:
1 <= T <= 100
Example:
Input:
2
rahul garg
ankit kitan Output:
NOT ANAGRAM ANAGRAM

Ans. C++ program:-

#include <iostream>
#include <string>
#include <algorithm>

bool isAnagram(const std::string& first_name, const std::string& last_name) {


// Remove spaces and convert strings to lowercase
std::string first_name_copy = first_name;
std::string last_name_copy = last_name;
first_name_copy.erase(std::remove_if(first_name_copy.begin(), first_name_copy.end(),
::isspace), first_name_copy.end());
last_name_copy.erase(std::remove_if(last_name_copy.begin(), last_name_copy.end(),
::isspace), last_name_copy.end());
std::transform(first_name_copy.begin(), first_name_copy.end(), first_name_copy.begin(),
::tolower);
std::transform(last_name_copy.begin(), last_name_copy.end(), last_name_copy.begin(),
::tolower);

// Sort characters in both names


std::sort(first_name_copy.begin(), first_name_copy.end());
std::sort(last_name_copy.begin(), last_name_copy.end());

// Check if sorted names are equal


return first_name_copy == last_name_copy;
}

int main() {
int num_tests;
std::cin >> num_tests;
std::[Link](); // ignore newline character

for (int i = 0; i < num_tests; ++i) {


std::string first_name, last_name;
std::getline(std::cin, first_name, ' ');
std::getline(std::cin, last_name);

if (isAnagram(first_name, last_name)) {
std::cout << "ANAGRAM" << std::endl;
} else {
std::cout << "NOT ANAGRAM" << std::endl;
}
}
return 0;
}
This C++ program performs the same functionality. You can compile and run it on your local
machine or any online C++ compiler. Just input the number of test cases followed by each test
case containing the first and last names of the student. The program will output "ANAGRAM" if
the names are anagrams of each other, and "NOT ANAGRAM" otherwise.

2. Write a program in C++ to convert a decimal number into binary without using an array with a
friend function.
Create a Class Decimal_Number having members decimal_no and binary_no with Constructor
to input decimal number and compute its binary number. Use a function friend int
show(Decimal_Number) as a friend to call an object of a class Decimal_Number to display the
binary number.

Ans. #include <iostream>


class Decimal_Number {
private:
int decimal_no;
int binary_no;

// Function to convert decimal to binary


void convertToBinary() {
int remainder, i = 1;
binary_no = 0;

while (decimal_no != 0) {
remainder = decimal_no % 2;
decimal_no /= 2;
binary_no += remainder * i;
i *= 10;
}
}

public:
// Constructor to input decimal number and compute its binary number
Decimal_Number(int decimal) {
decimal_no = decimal;
convertToBinary();
}

// Friend function to display the binary number


friend int show(Decimal_Number obj);
};

// Friend function definition to display the binary number


int show(Decimal_Number obj) {
return obj.binary_no;
}

int main() {
int decimal_input;
std::cout << "Enter a decimal number: ";
std::cin >> decimal_input;

Decimal_Number obj(decimal_input);

std::cout << "Binary equivalent: " << show(obj) << std::endl;


return 0;
}

You might also like