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
|
#include <iostream>
using namespace std;
struct Collection{
public:
char upper[100],lower[100]; //Modified: two arrays, of size 100, with value types of char
//Used to store upper case characters and lowercase characters.
int upp, low; //Two counters to determine number of values in each array.
Collection(){upp=low=0;} //Default Constructor: defaulting data to 0, for safe use.
};
void parseString(string str, Collection &C) //Our function to check through the string for upper and lower characters.
{
//Parse the string for uppers, lowers:
for(unsigned int i=0;i<str.size();i++) //Continue this loop while "i" less than the strings size.
{
if(str[i]>='A' && str[i]<='Z') //Check if the character is between 'A' and 'Z'; UPPERCASE
{
C.upper[C.upp]=str[i]; //IF so, assign our uppercase array, the character.
++C.upp; //Increment the upp counter; we have one letter more now!
}
else if(str[i]>='a' && str[i]<='z')
{
C.lower[C.low]=str[i]; //IF so, assign our uppercase array, the character.
++C.low; //Increment the low counter; we have one letter more now!
}
}
}
void printString(char ch[],int j) //Our function to print the characters in our array
{ //The "j" argument being the limit for our following loop:
for(int i=0;i<j;i++) //Loop while "i" is less than "j"; we only have j elements in this array.
cout<<ch[i]; //Print character at index "i"
cout<<endl; //Make a newline and flush the buffer.
}
int main(){
string str; //We're are using std::string to store the original string; not necessary,
//A c string of char *str; would do just as well, but requires a lib for strlen();
Collection C; //Creating our Collection object, and calling it "C".
cout<<"Enter a sentence with uppercase and lowercase characters: \n\n: "; //Prompt user for the string.
getline(cin, str); //Use getline to get characters, including spaces, till "enter" is pressed.
cout<<endl; //Make a newline and flush the buffer.
parseString(str,C); //Call the parseString function to create the lower and upper arrays.
printString(C.upper,C.upp); //Print an array, in this case: upper; passing upper(array) and upp(size) as arguments.
printString(C.lower,C.low); //Print an array, in this case: lower; passing lower(array) and low(size) as arguments.
cout<<"Custom exit: Press enter: "; //Console exit prompt, in case not ran from an IDE.
cin.get(); //Wait for user to enter return key ("enter").
return 0;
}
|
Procedural break down:
1) We first as the user for characters, till an "enter" was pressed.
2) We then operate on the string, looking for ASCII characters, in ranges of 'A' to 'Z' and 'a' to 'z'. http://www.asciitable.com/
3) If we find any, we copy its data into the appropriate array we had created for it; upper or lower. After each copy, we would increase the counter maintaining its usable size; anything beyond the counter hasn't been initialized and should not be used. Repeatedly doing this until we've checked the whole string entered by the user.
4) After that we will be printing the data from each array. To reduce code writing, we have created a function that takes an array of characters, and an integer, which tells us when to stop.
We should print the data and finish our task.
We finish up the program by asking the user to press enter, in case they are operating, directly, from the console and might possibly miss the output.
Receive the users input, and return 0 for a successful completion of the program.
The end.
*Changes: I've decided to change the string arrays in the object to character arrays since we are only parsing for single characters; string didn't hurt, but they weren't necessary for our case. Same applies to the print string function, it is now expecting an array of characters.
The main programs string variable could be changed into a c string, using pointer to characters, but I did not do so. If so, the programmer should include the <cstring> library and use
strlen(str)
instead of
str.size()
inside the parse strings function.