Skip to content

Instantly share code, notes, and snippets.

/Main

Created February 19, 2015 17:23
Show Gist options
  • Save anonymous/33c380881f145e9383fc to your computer and use it in GitHub Desktop.
Save anonymous/33c380881f145e9383fc to your computer and use it in GitHub Desktop.
#include <iostream>
#include <cassert>
#include <string>
using namespace std;
const char EOI('.');
const char EOT('#');
const int MAX(10);
int main() {
char readInCommand();
void executeCommand(char, char[]);
void displayMenu();
char text[MAX + 1] = { EOT };
displayMenu();
char command(readInCommand());
while (command != '0')
{
executeCommand(command, text);
displayMenu();
command = readInCommand();
}
cout << "\n\n";
system("pause");
return 0;
}
void displayMenu() {
cout << "\n\n\n ________MENU________\n";
cout << "\n STOP 0";
cout << "\n Input text 1";
cout << "\n Output text 2";
cout << "\n Length of text 3";
cout << "\n Put text in uppercase 4 - Works";
cout << "\n Count duplicates of given letter 5 - Works";
cout << "\n Remove all occurrences of given letter 6 - Sort of Works?";
}
char readInCommand() {
cout << "\n\nENTER YOUR COMMAND: ";
string contents;
getline(cin, contents);
return toupper(contents[0]);
}
void executeCommand(char option, char text[]) {
void readInText(char text[], int size);
void printOutText(const char text[]);
void putInUppercase(char text[]); // For Option 4
int countOccurrences(const char text[], char letter); // For Option 5
char removeAllOccurrences(char text[], char letter); // For Option 6
char readInLetter();
bool isValidText(const char text[]);
int length(const char[]);
assert(isValidText(text));
switch (option)
{
//Option 1
case '1':
readInText(text, MAX);
break;
//Option 2
case '2':
printOutText(text);
break;
//Option 3
case '3':
cout << "\nThere are " << length(text) << " letters in text";
break;
//Option 4
case '4':
cout << "\n";
putInUppercase(text);
break;
//Option 5
case '5':
{
const char letter(readInLetter());
int numberOfOccurrences(countOccurrences(text, letter));
cout << "\nThe text contains " << numberOfOccurrences << " duplicate(s) of the letter '" << letter << "'\n";
}
break;
//Option 6
case '6':
{
const char letter(readInLetter());
char removeAllOccurrence(char text, char letter);
}
break;
default: cout << "\nNOT IMPLEMENTED YET OR INVALID COMMAND, TRY AGAIN";
}
}
bool isValidText(const char text[]) {
bool found(false);
int pos(0);
while ((pos <= MAX) && (!found)) {
if (text[pos] == EOT)
found = true;
++pos;
}
return found;
}
char readInLetter() {
cout << "\nEnter a letter: ";
string contents;
getline(cin, contents);
return contents[0];
}
//Option 1
void readInText(char text[], int size) {
cout << "\nEnter the text (ending with '.'): ";
string contents;
getline(cin, contents);
int index(0);
char character(contents[index]);
while ((character != EOI) && (index < size)) {
text[index] = character;
++index;
character = contents[index];
}
text[index] = EOT;
}
//Option 2
void printOutText(const char text[]) {
assert(isValidText(text));
cout << "\nThe text is: ";
int index(0);
while (text[index] != EOT) {
cout << text[index];
++index;
}
cout << endl;
}
//Option 3
int length(const char text[]) {
assert(isValidText(text));
int index(0);
while (text[index] != EOT)
++index;
return index;
}
//Option 4
void putInUppercase(char text[])
{
for (int i = 0; text[i]!= EOT; i++)
{
text[i] = toupper(text[i]);
cout << text[i];
}
}
//Option 5
int countOccurrences(const char text[], char letter)
{
int numberOfOccurrences(0);
int i(0);
while (text[i] != EOT)
{
if (text[i] == letter)
{
numberOfOccurrences++;
}
i++;
}
return numberOfOccurrences;
}
// Option 6
void removeAllOccurrence(char text[], char letter)
{
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment