KeeganT

Ass413

Sep 21st, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.50 KB | None | 0 0
  1. #include <iostream>
  2. #include <windows.h>
  3. #include <math.h>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. //major credit goes to xX0T1Xx
  9.  
  10. //function to count number of characters
  11. int characters(string text)
  12. {
  13.     return text.length();
  14. }
  15. //function to count number of words
  16. int words(string text)
  17. {
  18.     int words = 0;
  19.     if(text[0]>0&&text[0]!=' ')words++;
  20.     for (int c=0;c<text.length();c++)if(text[c]==' '&&text[c+1]!=' '&&text[c+1]!=NULL)words++;
  21.     return words;
  22. }
  23. //function to list each words
  24. string eachWords(string text)
  25. {
  26.     string output;
  27.     for (int c=0;c<text.length();c++)
  28.     {
  29.         if(text[c]!=' ')
  30.         {
  31.             output+=text[c];
  32.         } else if (text[c]==' '&&text[c+1]!=' ')
  33.         {
  34.             output+=", ";
  35.         }
  36.     }
  37.     return output;
  38. }
  39. //function to reverse the text
  40. string rvs(string text)
  41. {
  42.     string newText;
  43.     for (int c=text.length()-1;c>-1;c--)newText+=text[c];
  44.     return newText;
  45. }
  46. //function to count uppercase letters
  47. int uppercaseLetters(string text)
  48. {
  49.     //65-90 (ASCII value for uppercase letters)
  50.     int capitalLetters = 0;
  51.     for (int c=0;c<text.length();c++)if((int)text[c]>=65&&(int)text[c]<=90)capitalLetters++;
  52.     return capitalLetters;
  53. }
  54. //function to count lowercase letters
  55. int lowercaseLetters(string text)
  56. {
  57.     //97-122(ASCII value for lowercase letters)
  58.     int lowercaseLetters = 0;
  59.     for (int c=0;c<text.length();c++)if((int)text[c]>=97&&(int)text[c]<=122)lowercaseLetters++;
  60.     return lowercaseLetters;
  61. }
  62. //function to count the number of punctuation and symbols
  63. int punctuation(string text)
  64. {
  65.     string chars = "~!@#$%^&*()_+|}{:'?<>`-=\][;,./";
  66.     int punctuationChars = 0;
  67.     for(int c=0;c<text.length();c++)
  68.     {
  69.         if(text[c]=='"')
  70.         {
  71.             punctuationChars++;
  72.         } else {
  73.             for(int k=0;k<chars.length();k++)if(text[c]==chars[k])punctuationChars++;
  74.         }
  75.     }
  76.     return punctuationChars;
  77. }
  78. //ridiculously long function to find the middle word(s)
  79. string middleWord(string text)
  80. {
  81.     int midWord = words(text)/2;
  82.     if (words(text)%2==1)midWord++;
  83.     string midsWord;
  84.     int currentWord = 1;
  85.     bool hasHitLetter=false;
  86.     for (int c=0;c<text.length();c++)
  87.     {
  88.         if(text[c]!=' ')hasHitLetter=true;
  89.  
  90.         if (words(text)%2==1)if(currentWord==midWord&&text[c]!=' ')midsWord+=text[c];
  91.         if (words(text)%2==NULL)if(currentWord==midWord||currentWord==midWord+1)midsWord+=text[c];
  92.         if(text[c]==' '&&text[c+1]!=' '&&text[c+1]!=NULL&&hasHitLetter==true)currentWord++;
  93.     }
  94.     return midsWord;
  95. }
  96. //function to find the longest word
  97. string longWord(string text)
  98. {
  99.     string tempWord="";
  100.     string longestWord="";
  101.     for(int lw=0;lw<text.length();lw++)
  102.     {
  103.         if(text[lw]!=' ')
  104.         {
  105.             tempWord+=text[lw];
  106.         }
  107.         else
  108.         {
  109.             tempWord="";
  110.         }
  111.         if(tempWord.length()>longestWord.length())
  112.         {
  113.             longestWord=tempWord;
  114.         }
  115.     }
  116.     return longestWord;
  117. }
  118. //function to find the shortest word
  119. string shortWord(string text)
  120. {
  121.     vector<string> words;
  122.     for (int c=0;c<text.length();c++)if(text[c]!=' ')if(text[c+1]==' '||text[c+1]==NULL)words.push_back("");
  123.     int wordCount = words.size();
  124.     if(wordCount>0)
  125.     {
  126.         int currentWord=0;
  127.         for (int c=0;c<text.length();c++)
  128.         {
  129.             if (text[c]==' '&&text[c+1]!=' ')currentWord++;
  130.             if (text[c]!=' ')words[currentWord]+=text[c];
  131.         }
  132.     } else return NULL;
  133.     int smallWordLen = text.length();
  134.     string smallestWord;
  135.     for (int w=0;w<wordCount;w++)
  136.     {
  137.         if (words[w].length()<smallWordLen)
  138.         {
  139.             smallestWord=words[w];
  140.             smallWordLen=words[w].length();
  141.         }
  142.     }
  143.     return smallestWord;
  144. }
  145. //function to find words containing 'the'
  146. string theCount(string text)
  147. {
  148.     vector<string> words;
  149.     int currentWord = 0;
  150.     words.push_back("");
  151.     for (int c=0;c<text.length();c++)
  152.     {
  153.         if(text[c]==' '&&text[c+1]!=' ')
  154.         {
  155.             currentWord++;
  156.             words.push_back("");
  157.         }
  158.         words[currentWord]+=text[c];
  159.     }
  160.     vector<string> words2;
  161.     int currentWord2 = 0;
  162.     string currentWord3;
  163.     for (int w=0;w<words.size();w++)
  164.     {
  165.         words2.push_back("");
  166.         currentWord3 = words[w];
  167.         for(int wc=0;wc<currentWord3.length();wc++)
  168.         {
  169.             if(currentWord3[wc]!=' ')
  170.                 words2[currentWord2]+=currentWord3[wc];
  171.         }
  172.         currentWord2++;
  173.     }
  174.     vector<string> theWords;
  175.     int currentWord4 = 0;
  176.     string currentWord5;
  177.     bool hasAdded = false;
  178.     int theWordsCount = 0;
  179.     for (int word=0;word<words2.size();word++)
  180.     {
  181.         currentWord5 = words2[word];
  182.         for (int c=0;c<currentWord5.length();c++)
  183.         {
  184.             if (currentWord5[c]=='t'&&currentWord5[c+1]=='h'&&currentWord5[c+2]=='e'&&hasAdded==false)
  185.             {
  186.                 theWords.push_back(currentWord5);
  187.                 hasAdded = true;
  188.             }
  189.         }
  190.         hasAdded = false;
  191.         currentWord4++;
  192.     }
  193.     string returnData;
  194.     for (int theword=0;theword<theWords.size();theword++)
  195.     {
  196.         if (theword!=theWords.size()-1)
  197.         {
  198.             returnData+=theWords[theword]+", ";
  199.         } else {
  200.             returnData+=theWords[theword];
  201.         }
  202.     }
  203.     return returnData;
  204. }
  205. int main()
  206. {
  207.     string sentence;
  208.     cout<<"Enter a sentence: ";
  209.     getline(cin, sentence);
  210.     cout<<"Total Characters (Including Spaces): "<<characters(sentence)<<endl;
  211.     cout<<"Total Word Count: "<<words(sentence)<<endl;
  212.     cout<<"Words: "<<eachWords(sentence)<<endl;
  213.     cout<<"Sentence Reversed: "<<rvs(sentence)<<endl;
  214.     cout<<"Number of Capital Letters: "<<uppercaseLetters(sentence)<<endl;
  215.     cout<<"Number of Lowercase Letters: "<<lowercaseLetters(sentence)<<endl;
  216.     cout<<"Number of Punctuation Marks / Symbols: "<<punctuation(sentence)<<endl;
  217.     cout<<"Middle Word(s): "<<middleWord(sentence)<<endl;
  218.     cout<<"Longest word: "<<longWord(sentence)<<" ("<<longWord(sentence).length()<<" character(s) long)"<<endl;
  219.     cout<<"Shortest word: ";
  220.     if (words(sentence)==1)
  221.         {cout<<sentence<<" ("<<sentence.length()<<" character(s) long)"<<endl;}
  222.     else
  223.         {cout<<shortWord(sentence)<<" ("<<shortWord(sentence).length()<<" character(s) long)"<<endl;}
  224.     cout<<"Words containing 'the': "<<theCount(sentence)<<endl;
  225.  
  226.     system("pause>nul");
  227.     return 0;
  228. }
Add Comment
Please, Sign In to add comment