Answer to Question #199319 in C++ for Ahmed Ali

Question #199319

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.


1
Expert's answer
2021-05-27T06:25:32-0400
#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;
	
	
}





Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog