Answer to Question #150511 in C++ for Mahnoor Arif

Question #150511
Start with the String class from the Str example. Add a member function called upit() that converts the string to all uppercase. You can use the toupper() library function, which takes a single character as an argument and returns a character that has been converted (if necessary) to uppercase. This function uses the CCTYPE header file. Write some code in main() to test upit().

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstring> //for strcpy(), etc
#include <cctype> //for toupper()
using namespace std;
1
Expert's answer
2020-12-11T18:40:07-0500
#include <stdlib.h>
#include <iostream>
#include <stdio.h>
#include <cstdlib>
#include <cstring>
#include <cctype>
using namespace std;

class String {
private:
char *pStr;
int _size;
public:
String() {
pStr = NULL;
_size = 0;
}
void Upit()
{
for (int i = 0; i < pStr[i]; i++)
pStr[i] = toupper(pStr[i]);
}
String(char *str) {
_size = strlen(str) + 1;
pStr = new char[_size];
strcpy(pStr, str);
}
int size() {
return _size;
}
~String() {
delete []pStr;
}
friend ostream &operator; << (ostream &os;, String &obj;) {
for(int i = 0; i < obj.size(); i++)
os <<obj.pStr[i];
return os;
}
};
int main(){
String str("Some String");
str.Upit();
cout << str;
system&#40;"pause"&#41;;
return 0;
}

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