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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
|
#include <cstdlib>
#include <iomanip>
#include <iostream>
using namespace std;
char Func_menu(); //Menu Function prototype //
void swapChars(char*, char, char); //character swap function prototype
int countChar(char*, char); //character counter function prototype
void get_sentence(char*); //function to aquire string from user
void get_sentence(char* userline) //get_sentence function header
{
cout << "\n\nWelcome To Ms. String's String Editor!"
"\n=============================="
"\nPlease Enter A String:\n"; //get string from user
cin.get(userline, 80);
}
char Func_menu() //Definition header
{
char valid_entry; //for loop to ensure valid menu selection
char selection; //i/o menu choices
/*
This function allows user to select from initial menu options,
and then passes their choice back to the main() function
which then calls the appropriate string modifier function based
on users choice.
*/
do
{
selection = 'x';
if(selection == 'x')
{
cout << "\nString Editor"
"\n---------------------------"
"\nPlease Select from the Options Below:\n"
"\n---------------------------"
"\n(A)Substitute Character"
"\n(B)Count Character"
"\n(E)Exit Program"
"\nPlease Make a Selection Using Letters in Parenthesis:\n\n";
cin >> selection;
}
switch(selection)
{
case 'a':
case 'A':
valid_entry == 'y';
return selection;
break;
case 'b':
case 'B':
valid_entry == 'y';
return selection;
break;
case 'e':
case 'E':
valid_entry == 'y';
return selection;
break;
default:
valid_entry = 'n';
cout << "\nEntry Not Recognized, Please Retry\n";
system("PAUSE");
break;
}
}while(valid_entry == 'n');
}
void swapChars(char* str, char c1, char c2)
/*
The swapChars() function scans the user inputted
string for oldchar, and then swaps it with
user inputted newchar.
*/
{
for (int i=0;i<80;i++) if (str[i]==c1) str[i]=c2;
}
int countChar(char* str, char c)
/*
The countChar() function counts how many times a
particular char appears in the string, then replaces
each occurance of that char with the number order in which it appeared.
*/
{
int count=0;
for (int i=0;i<80;i++)
if (str[i]==c)
{
count++;
if (count<=9)
{str[i]=count+48;
}
}
return count;
}
int main()
{
char newstring = ' '; //for user to choose to change the entered string
char go_again = 'y'; //char for invalid entry loop check
char countedchar; //the character to be counted
int cuanto; //how many times the character was counted
char oldchar; //character to be replaced
char newchar; //replacement character
char sentence[80]; //string array
char menu_choice = ' '; //entry to continue or not
do{
get_sentence(sentence);
cout << "\nYou Entered: " << sentence <<"\n"; //show user entry
menu_choice = Func_menu(); //calls menu function
switch(menu_choice)
{
default:
system("PAUSE");
cout << "\nInvalid Entry, Please Retry.\n";
break;
case 'A':
case 'a':
cout << "\nSubstitute Character\n"
"==========================\n"
"Enter Character To Be Scanned:\n";
cin >> oldchar;
cout << "\nEnter Character To Be Substituted:\n";
cin >> newchar;
swapChars(sentence, oldchar, newchar);
cout <<"\nThe Updated String Is:\n"
<< sentence << "\n\n";
system("PAUSE");
break;
case 'b':
case 'B':
cout << "\nCharacter Count\n"
"======================\n"
"Enter Character To Be Counted:\n";
cin >> countedchar;
cuanto = countChar(sentence, countedchar);
cout << "\nThe Revised String Is:\n"
<< sentence <<"\n"
"The Character " << countedchar <<
" appeared " << cuanto << " Times.\n\n";
system("PAUSE");
break;
case 'e':
case 'E':
cout << "SESSION TERMINATED, ";
system("PAUSE"); //DISPLAY END MESSAGE
return 0;
break;
} //end switch statement
}while( (menu_choice != 'e') || (menu_choice != 'E') );
system("PAUSE");
return EXIT_SUCCESS;
}
|