Write a program using class “string”. Pass a string as arguments to the constructor of the class and initialize it in a data member “str”. The class should have the following member function:
◦ One member function “strlength” to find and return the length of string.
◦ One member function ‘lowercase’ to convert the string into lowercase.
◦ One member function ‘uppercase’ to convert the string into uppercase.
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
class String{
public:
char* str;
String(char* arr)
{
this->str = arr;
}
int strlength(char* s)
{
int i=0;
int count =0;
while(s[i] != '\0')
{
i++;
count++;
}
return count;
}
void lowercase(char* s)
{
for(int i=0; s[i]!='\0'; i++)
{
if(s[i] >='A' && s[i] <= 'Z')
{
s[i] = s[i]+32;
}
}
}
void uppercase(char* s)
{
for(int i=0; s[i]!='\0'; i++)
{
if(s[i] >='a' && s[i] <= 'z')
{
s[i] = s[i]-32;
}
}
}
};
int main()
{
cout<<"Enter a string : ";
char s[100];
cin.getline(s, 100);
String s1(s);
cout<<endl<<endl;
cout<<"Length of string is : ";
cout<<s1.strlength(s)<<endl;
cout<<endl<<"Converting entered string to Upper case : ";
s1.lowercase(s);
cout<<s<<endl;
cout<<endl<<"Converting entered string to Upper case : ";
s1.uppercase(s);
cout<<s;
}
Comments
Leave a comment