BCA
Object Oriented Programming using C++ Practical
Madras University
Program 7
Design a class called cString to represent a string data type. Create a data member in
the class to represent a string using an array of size 100.
Write the following functionality as
member functions:
a. Copy Constructor
b. Concatenate two strings
c. Find the length of the string
d. Reversing a string
e. Comparing two strings
SOURCE CODE:
#include <iostream>
#include <cstring>
using namespace std;
class cString {
private:
char str[100]; // Array to hold the string, fixed size of 100
public:
// Default Constructor
cString() {
str[0] = '\0'; // Initialize with empty string
}
// Parameterized Constructor
cString(const char* s) {
strncpy(str, s, 99);
str[99] = '\0'; // Ensure null termination
}
// Copy Constructor
cString(const cString& other) {
strncpy(str, other.str, 99);
str[99] = '\0'; // Ensure null termination
}
// Function to concatenate two strings
void concatenate(const cString& other) {
strncat(str, other.str, 99 - strlen(str)); // Append with max size consideration
str[99] = '\0'; // Ensure null termination
}
// Function to find the length of the string
int length() const {
return strlen(str);
}
// Function to reverse the string
void reverse() {
int len = strlen(str);
for (int i = 0; i < len / 2; ++i) {
swap(str[i], str[len - 1 - i]);
}
}
// Function to compare two strings
bool compare(const cString& other) const {
return strcmp(str, other.str) == 0;
}
// Function to print the string
void print() const {
cout << str << std::endl;
}
// Function to read string from user
void readFromUser() {
cout << "Enter a string (max 99 characters): ";
cin.getline(str, 100);
}
};
int main() {
cString s1,s2;
// Read strings from user
s1.readFromUser();
s2.readFromUser();
// Using Copy Constructor
cString s3 = s1;
// Concatenate strings
s1.concatenate(s2);
// Print strings
cout << "s1: ";
s1.print();
cout << "s2: ";
s2.print();
cout << "s3: ";
s3.print();
// Find length of s1
cout << "Length of s1: " << s1.length() << endl;
// Find length of s2
cout << "Length of s2: " << s2.length() << endl;
// Reverse s1
s1.reverse();
cout << "Reversed s1: ";
s1.print();
// Reverse s2
s2.reverse();
cout << "Reversed s2: ";
s2.print();
// Compare s1 and s3
cout << "s1 and s3 are " << (s1.compare(s3) ? "equal" : "not equal") << endl;
return 0;
}
OUTPUT:
Enter a string (max 99 characters): hello
Enter a string (max 99 characters): world
s1: helloworld
s2: world
s3: hello
Length of s1: 10
Length of s2: 5
Reversed s1: dlrowolleh
Reversed s2: dlrow
s1 and s3 are not equal
Explanation:
Data Member: The class cString
has a private data member str
which is a character array of size 100 to hold the string.
Constructors:
- Default Constructor initializes
str
with an empty string. - Parameterized Constructor initializes
str
with the given C-style string. - Copy Constructor copies the string from another
cString
object.
Concatenate Function: The concatenate
function appends the string from another cString
object to the current string. It uses strncat
to ensure that the array size limit is respected.
Length Function: The length
function returns the length of the string using strlen
.
Reverse Function: The reverse
function reverses the string by swapping characters from the start and end moving towards the center.
Compare Function: The compare
function compares the string with another cString
object using strcmp
and returns true
if they are equal.
Print Function: The print
function prints the string to the standard output.
No comments:
Post a Comment