Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. #include <iostream>
  2. using std::cout;
  3. using std::cin;
  4. using std::endl;
  5. #include <string>
  6. using std::string;
  7. using std::getline;
  8.  
  9. int letters[26]; //global variables - two arrays
  10. int wordLength[10];
  11.  
  12. void countLetters(string str); //function prototypes
  13. void countWords(string str);
  14. string toUpperCase(string str);
  15. void printTables();
  16.  
  17. int main() { //main function where user enters phrase
  18. string line;
  19. cout<<"Enter a phrase: ";
  20. getline(cin, line);
  21. line = toUpperCase(line);
  22. countLetters(line);
  23. countWords(line);
  24. printTables();
  25.  
  26. system("PAUSE");
  27. return 0;
  28. }
  29. //function declarations
  30. void countLetters(string line){
  31. //to initialize all the items inside table to zero;
  32. for(int i=0; i<26; i++){
  33. letters[i] = 0;
  34. }
  35.  
  36. int Ais65 = 65; //ASCII code of capital A is 65
  37. for(int i=0; i<line.length(); i++)
  38. {
  39. if(isalpha(line[i]))
  40. {
  41. int alphaNumber = line[i];
  42. alphaNumber = alphaNumber - 65;
  43. letters[alphaNumber]++; //we add one to number of occurrences of each letter of the alphabet passed through the program
  44. }
  45. }
  46.  
  47. }
  48. void countWords(string str)
  49. {
  50. for(int i=0; i<10; i++){
  51. wordLength[i] = 0;
  52. }
  53. int length = 0;
  54. for(int i = 0; i < str.length(); i++){//counts the number of words of each length-type
  55. if(isalpha(str[i])){
  56. length++;
  57. }
  58. else{
  59. if(length != 0 && length < 11){ //resets word length for next word
  60. wordLength[length-1]++;
  61. length = 0;
  62. }
  63. }
  64. }
  65. //just to make sure we get the last word
  66. if(length != 0 && length < 11){
  67. wordLength[length-1]++;
  68. }
  69. }
  70.  
  71. string toUpperCase(string str){ //making the user-entered phrase into all capital letters
  72. string tmp = "";
  73. for(int i=0; i<str.length(); i++)
  74. {
  75. tmp += toupper(str[i]);
  76. }
  77. return tmp;
  78. }
  79.  
  80. void printTables(){ //prints the tables for the program
  81. cout<<"Letter \t Number of Occurrences\n";
  82. char ch;
  83. for(int i = 0; i < 26; i++){
  84. ch = i + 65;
  85. cout<<" "<<ch<<" "<<letters[i]<<endl;
  86. }
  87.  
  88. cout<<"Word Length \t Occurrences\n";
  89. for(int i = 0; i < 10; i++){
  90. cout<<" "<<i+1<<" "<<wordLength[i]<<endl;
  91. }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement