#include #include using namespace std; // average word length float average_word_length(const char *string){ int current_word_length = 0; int word_count = 0; int letter_total = 0; for(int i = 0; string[i]; i++){ if(string[i] == ' '){ // We finished a word word_count++; letter_total += current_word_length; current_word_length = 0; } else current_word_length++; } if(current_word_length){ letter_total += current_word_length; word_count++; } return (float)letter_total / word_count; } // convert to title case void title_case(char *string){ if(islower(*string)) // Could also call it string[0] *string = toupper(*string); for(string++; *string; string++) if(*(string - 1) == ' ') *string = toupper(*string); else if(isalpha(*string)) *string = tolower(*string); } // encrypt with shift of 13 void encrypt(char *string){ for(;*string; string++) if(isalpha(*string)){ if(islower(*string)){ if(*string + 13 > 'z') *string -= 26; *string += 13; } else { if(*string + 13 > 'Z') *string -= 26; *string += 13; } } } int main(){ char phrase[] = "There was once a fish that fell off a roof into an Apple"; const char *phrase2 = "A monkey caught the fish and put it in a bucket"; phrase[5] = 'Z'; cout << phrase << endl; // phrase2[5] = 'Z'; // Causes segfault cout << phrase2 << endl; cout << average_word_length("bear goat deer") << endl; cout << "Phrase average word length: " << average_word_length(phrase) << endl; title_case(phrase); cout << "Phrase, in title case: " << phrase << endl; //title_case("this is a string"); // Can't edit a static string encrypt(phrase); cout << "Encrypted: " << phrase << endl; encrypt(phrase); cout << "Decrypted: " << phrase << endl; return 0; }