Character Converter Class

Pages: 123
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.
Last edited on
I don't want someone to do it for me, I just need help starting off.
Do you know how to create a class?

i.e. Do you understand the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class testclass {
  int i; 
public:
  int get_i();
  void put_i(int j);
};

int testclass::get_i()
{
  return i;
}

void testclass::put_i(int j)
{
  i = j;
}

int main()
{
  testclass mytestclass;

  mytestclass.put_i(10);
  cout << mytestclass.get_i() <<endl;

  return 0;
}
I understand a little bit lol. I'm new to programming :D
First thing you need to do is make a skeleton for your CharConverter class.

i.e.
1
2
3
4
5
6
class CharConverter {

// fill in the blanks
// you know you need methods (also called member functions) called 'uppercase' and 'properWords'

};
Last edited on
Yes, I got that part already :D I just don't know the codes and stuff to put in there :p
Do would start with putting the function declarations inside the class

1
2
3
public:
  RETURNTYPE uppercase(PARAMETERS);
  RETURNTYPE properWords(PARAMETERS);


You will need to decide the return type and parameters (if any) for yourself.
Here's something my teacher gave me.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//count the number of uppercase characters

#include<iostream>
#include<string>
using namespace 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.
Yes, but I don't know the code to do that or how to do that :p The book I have isn't much help because it's hard to follow and read.
Can you make me a program to reverse a string that a user inputs?
Like if they type hey itll output yeh?
Yeah, like that.
int main()
{
string s;
getline(cin, s);
reverse(s.begin(), s.end());
cout << s << endl;
return 0;
}

is that how to do it?
I looked up on how to do it and made it from what I found.
I found something online that reverses the string.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <cstring>  //For string length: strlen();
using namespace 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;
}
Did you even try your code? I would spend a day with a good beginners book on C++ before doing anything else.

Take a look at http://www.cplusplus.com/doc/tutorial/ and spend at least a day with it.
Ya I tried it after I posted it. It doesnt do anything lol.
If you can't even reverse a string yourself, you're not going to be able to do this assignment.
That's why I'm asking for help. I am completely new to this.
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.
Last edited on
Pages: 123