I need to create a character converter class. Here's the instructions:
1 2 3 4 5 6 7 8 9
Create a CharConverter class that performs various operations on strings. It should have the following two public member functions to start with.
The uppercase member function accepts a string and returns a copy of it with all lowercase letters converted to uppercase. If a character is already uppercase,
or is not a letter, it should be left alone.
The properWords member function accepts a string of words separated by spaces and returns a copy of it with the first letter of each word converted to uppercase.
Write a simple program that uses the class. It should prompt the user to input a string. Then it should call the properWords function and display the
resulting string. Finally, it should call the uppercase function and display the resulting string. The program should loop to allow additional strings to be
converted and displayed until the user chooses to quit.
I know how to loop it to ask the user if they want to do it again. That's the easy part lol. I tried googling this but I can't find anything helpful. I'm not sure on how to start this code :( Any help please.
//count the number of uppercase characters
#include<iostream>
#include<string>
usingnamespace std;
//prototypes
void getInput(string& in);
class CountEm
{
private:
int count; //holds the count of uppercase characters
public:
CountEm();
void countUpper(string);
void display();
};
/***********Method definitions********************/
CountEm::CountEm()
{
//initialize variables
count = 0;
}
void CountEm::countUpper(string s)
{
//find the length of the string
int size = s.length();
//loop through the string variable counting the number of uppercase characters
for (int i = 0; i < size; i++)
{
if (s[i] >= 'A' && s[i] <= 'Z')
count++;
}
}
void CountEm::display()
{
//display the value
cout << endl << endl;
cout << "The number of uppercase characters is: " << count << endl << endl;
}
/**********************Driver**********************/
int main()
{
//variables
string userInput; //holds the input from the user
CountEm string1; //object of class CountEm
getInput(userInput); //get input from the user
string1.countUpper(userInput); //count the number of uppercase characters
string1.display(); //display the result
return 0;
}
/*************Function Definitions*************************/
void getInput(string& in)
{
//prompt the user
cout << "Enter a string, press Enter when done" << endl;
getline(cin, in); //reads in everything until the enter key is pressed
}
I asked her if that's the start of the program. She said "Sure, you can use this, if you like. Just remember, for the uppercase function, you don't want to test for uppercase, but to make everything uppercase. In the proper words function, you'll want to set the 0 element uppercase. After that, you'll test if an element is equal to a space. If it is, the next letter will begin a new word. That's when you'll want to uppercase that letter. I hope that helps."
I'm not sure what she's talking about lol. I know I need to change the function to make the input uppercase. I'm not sure what 0 element uppercase is.
Hint: "The uppercase member function accepts a string and returns a copy of it with all lowercase letters converted to uppercase. If a character is already uppercase,
or is not a letter, it should be left alone.
The properWords member function accepts a string of words separated by spaces and returns a copy of it with the first letter of each word converted to uppercase." tells you everything you need to know.
#include <iostream>
#include <cstring> //For string length: strlen();
usingnamespace std;
int main()
{
//Declare an array of characters: (c string)
char cstring[20];
//Use cins getline function to retrieve a string from user:
cout<<"Enter the string\n\n: ";
cin.getline(cstring,20);
//Declare a variable of integer to store the size of array.
int strSize = strlen(cstring);
//Procedure: Reversing c string:
for(int i=0;i<strSize*0.5;++i)
{
char ch = cstring[i];
cstring[i] = cstring[(strSize-1)-i];
cstring[(strSize-1)-i] = ch;
}
//Print c string:
cout<<": "<<cstring;
//Console exit prompt: in the case of command line execution:
cout<<"\n\nPress enter to continue: ";
cin.get();
return 0;
}
I pointed you in the right direction, but you need a better grasp of the basics. Spend a day with a beginner's book on C++, or the tutorial I linked to earlier. I've had good luck with the library for understanding some things. Some textbooks are awful. I learned university chemistry from a different textbook than the one in the class.