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
|
/*practice program for 5b*/
#include <iostream>
#include <cstring>
#include <cctype>
void sort(char a[], int number_used);
void swap_values (char& v1, char& v2);
int index_of_smallest(const char a[], int start_index, int number_used);
void deleteRepeats(char a[], int& size);
bool findChar(char mark, char a[], int size);
using namespace std;
int main()
{
int i, len = 0, words = 0, letters = 0, mark = 0;
char userInput[200];
cout << "Enter a phrase with a '.' as a terminating character." << endl << endl;
cin.getline(userInput, 200);/*gets the userInput as a phrase*/
/*cout << userInput;*/
cout << endl;
len = strlen(userInput);/*counts the characters in the string*/
cout << "There are " << len << " characters in your phrase." <<endl << endl;
for (i = 0; i < len; i++)/*to find the number of words in userInput*/
{
userInput[i] = tolower(userInput[i]);/*removes the capial letters*/
if ((userInput[i] == ' ') || (userInput[i] == '.') || (userInput[i] == '\n'))
{
words++;/*calculates number of words based on spaces.*/
}
}
cout << "There are " << words << " words in your phrase." << endl << endl;
for (i = 0; i < len; i++)/*remove spaces and punctuation*/
{
if (userInput[i] != ' ' || userInput[i] != '.') /*QUESTION #1 I've tried a few combinations of this
adding '!' and ',' and '-'.
It only seems to use the ' '*/
{
cout << userInput[i];
}
}
sort(userInput, len);
cout << endl;
cout << endl;
cout << "Here are the letters used in your phrase: " << endl;
deleteRepeats(userInput, len);
for (i = 0; i < len; i++)
{
cout << userInput[i] << endl;
}
system ("pause");
return 0;
}/*end of main*/
/****Function Definitions****/
void sort(char a[], int number_used)
{
int index_of_next_smallest;
for (int index = 0; index < number_used - 1; index++)
{
index_of_next_smallest = index_of_smallest(a, index, number_used); /*sort function calls index_of_smalles*/
swap_values(a[index], a[index_of_next_smallest]); /*sort funcion calls swap_values function*/
}
}
void swap_values(char& v1, char& v2)
{
char temp;
temp = v1;
v1 = v2;
v2 = temp;
}
int index_of_smallest(const char a[], int start_index, int number_used)
{
int min = a[start_index], index_of_min = start_index;
for (int index = start_index + 1; index < number_used; index++)
{
if (a[index] < min)
{
min = a[index];
index_of_min = index;
}
}
return index_of_min;
}
bool findChar (char mark, char a[], int size) /*QUESTION #2 I need to add the number of individual
characters to the list of characters used.
I'm thinking a sum loop would work. Can't seem to place it in the right place. Any suggestions?*/
{
for (int i = 0; i < size; i++)
{
if (a[i] == mark) return true;
}
return false;
}
void deleteRepeats(char a[], int& posUsed)
{
int i;
int newSize =0;
for (i = 0; i < posUsed; i++)
{
if(!findChar(a[i], a, newSize))/*IMPORTANT - function call within a function calls the bool*/
{
a[newSize] = a[i];
newSize++;
}
}
posUsed = newSize;
}
|