[go: up one dir, main page]

0% found this document useful (0 votes)
20 views7 pages

425 Assignment

Uploaded by

faizur.raheeb
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)
20 views7 pages

425 Assignment

Uploaded by

faizur.raheeb
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
You are on page 1/ 7

North South University

Assignment-2
CSE425.9

Submitted by
Faizur Rahim Raheeb 2031043042

Submitted on
23th May 2024
1. Write a Program to Find the Length of the String Without using
strlen() Function.

Code:

test_string = "My name is Raheeb.”

length = 0

for char in test_string:

length += 1

print("The length of the string is:", length)

2. Write a Program to Check Palindrome

Code:

def is_palindrome(s):

s = s.replace(" ", "").lower()

return s == s[::-1]

test_string = "A man a plan a canal Panama"

print("Is the string a palindrome?", is_palindrome(test_string))


3. Write a Program to Implement the Concept of Operator and
Function Overloading

Operator overloading example:

class Point:

def __init__(self, x, y):

self.x = x

self.y = y

def __add__(self, other):

return Point(self.x + other.x, self.y + other.y)

def __str__(self):

return f"Point({self.x}, {self.y})"

p1 = Point(1, 2)

p2 = Point(3, 4)

p3 = p1 + p2

print(p3) # Output: Point(4, 6)


Function overloading example:

class Math:

def add(self, a, b, c=0):

return a + b + c

math_obj = Math()

print(math_obj.add(1, 2)) # Output: 3

print(math_obj.add(1, 2, 3)) # Output: 6

4. Write a Program for the Implementation of Stacks Using an Array using object
oriented modeling.

Code:

class Stack:

def __init__(self):

self.stack = []

def push(self, item):

self.stack.append(item)

def pop(self):

if not self.is_empty():
return self.stack.pop()

else:

return "Stack is empty"

def peek(self):

if not self.is_empty():

return self.stack[-1]

else:

return "Stack is empty"

def is_empty(self):

return len(self.stack) == 0

def size(self):

return len(self.stack)

# Test the class

my_stack = Stack()

my_stack.push(1)

my_stack.push(2)

my_stack.push(3)

print("Stack size:", my_stack.size())

print("Top element:", my_stack.peek())


print("Popped element:", my_stack.pop())

print("Stack size after pop:", my_stack.size())

5. Write a C++ Program to Print the Given String in Reverse Order


Using Recursion

Code:

#include <iostream>

using namespace std;

void printReverse(string str, int index) {

if (index < 0) {

return;

cout << str[index];

printReverse(str, index - 1);

int main() {

string test_string;

cout << "Enter a string: ";

getline(cin, test_string);
printReverse(test_string, test_string.length() - 1);

cout << endl;

return 0;

You might also like