KeeganT

Ass413 Redone

Oct 30th, 2017
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.42 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. ///word counter
  6. void wordCounter(string text)
  7. {
  8.     int wordCount=0;
  9.     if(text[0]>0&&text[0]!=' ')wordCount++;
  10.     for(int c=0;c<text.length();c++)if(text[c]==' '&&text[c+1]!=' '&&text[c+1]!=NULL)wordCount++;
  11.     cout<<"Total words: "<<wordCount<<endl;
  12. }
  13. ///word lister
  14. void wordLister(string text)
  15. {
  16.     string words;
  17.     for (int c=0;c<text.length();c++)
  18.     {
  19.         if(text[c]!=' ')words+=text[c];
  20.         else if (text[c]==' '&&text[c+1]!=' ')words+=", ";
  21.     }
  22.     cout<<"Each word: "<<words<<endl;
  23. }
  24. ///text reverser
  25. void textReverser(string text)
  26. {
  27.     string reversedText;
  28.     for (int c=text.length()-1;c>-1;c--)reversedText+=text[c];
  29.     cout<<"Text reversed: "<<reversedText<<endl;
  30. }
  31. ///uppercase counter
  32. void upperCount(string text)
  33. {
  34.     int capLetters=0;
  35.     for(int c=0;c<text.length();c++)if((int)text[c]>=65&&(int)text[c]<=90)capLetters++;
  36.     cout<<"Number of uppercase letters: "<<capLetters<<endl;
  37. }
  38. ///lowercase counter
  39. void lowerCount(string text)
  40. {
  41.     int lowLetters=0;
  42.     for(int c=0;c<text.length();c++)if((int)text[c]>=97&&(int)text[c]<=122)lowLetters++;
  43.     cout<<"Number of lowercase letters: "<<lowLetters<<endl;
  44. }
  45. ///punctuation counter
  46. void punctuationCounter(string text)
  47. {
  48.     string chars="~!@#$%^&*()_+|}{:'?<>`-=\][;,./";
  49.     int puncChars=0;
  50.     for(int c=0;c<text.length();c++)
  51.     {
  52.         if(text[c]=='"')puncChars++;
  53.         else for(int i=0;i<chars.length();i++)if(text[c]==chars[i])puncChars++;
  54.     }
  55.     cout<<"Number of punctuation marks: "<<puncChars<<endl;
  56. }
  57. ///middle word finder
  58. void middleWord(string text)
  59. {
  60.     string midword="", midword2="";
  61.     int firstSpace=text.find(" ",0);
  62.     int var1=0, var2=0, var3=0, var4=0, var5=0, var6=0, var7=0;
  63.     int spaceCount=0;
  64.     for(int c=0;c<text.length();c++)if(text[c]==' ')spaceCount++;
  65.     int middleSpace=spaceCount/2;
  66.     for(int c=0;c<=middleSpace;c++)
  67.     {
  68.         var1=text.find(" ",var2);
  69.         var2=var1+1;
  70.         if(c<middleSpace&&c>=middleSpace-1)break;
  71.     }
  72.     var3=text.find(" ",var2);
  73.     var4=var3-var2;
  74.     midword=text.substr(var2, var4);
  75.     if((spaceCount%2)==0)cout<<"The middle word is: "<<midword<<endl;
  76.     if((spaceCount%2)!=0)
  77.     {
  78.         var5=var3+1;
  79.         var6=text.find(" ",var5);
  80.         var7=var6-var5;
  81.         midword2=text.substr(var5,var7);
  82.         cout<<"The middle words are: "<<midword<<", "<<midword2<<endl;
  83.     }
  84. }
  85. ///longest word finder
  86. void longestWord(string text)
  87. {
  88.     string longWord="";
  89.     string tempWord="";
  90.     for(int c=0;c<text.length();c++)
  91.     {
  92.         if(text[c]!=' ')tempWord+=text[c];
  93.         else tempWord="";
  94.         if(tempWord.length()>longWord.length())longWord=tempWord;
  95.     }
  96.     cout<<"Longest word: "<<longWord<<" ("<<longWord.length()<<" character";if(longWord.length()!=1)cout<<"s";cout<<" long)"<<endl;
  97. }
  98. ///shortest word finder
  99. void shortestWord(string text)
  100. {
  101.     string tempWord="";
  102.     string shortWord=text;
  103.     string letter="";
  104.     for(int c=0;c<text.length();c++)
  105.     {
  106.         letter=text.substr(c,1);
  107.         if(letter!=" ")tempWord+=letter;
  108.         if(letter==" ")
  109.         {
  110.             if(tempWord.length()<shortWord.length())shortWord=tempWord;
  111.             tempWord="";
  112.         }
  113.     }
  114.     if(tempWord.length()<shortWord.length())shortWord=tempWord;
  115.     cout<<"The shortest word is: "<<shortWord<<" ("<<shortWord.length()<<" character";if(shortWord.length()!=1)cout<<"s";cout<<" long)"<<endl;
  116. }
  117. ///'The' counter
  118. void theList(string text)
  119. {
  120.     string theList="", tempWord="", letter="";
  121.     for(int c=0;c<text.length();c++)
  122.     {
  123.         letter=text.substr(c,1);
  124.         if(letter!=" ")tempWord+=letter;
  125.         if(letter==" ")
  126.         {
  127.             if(tempWord.find("the",0)!=string::npos)theList+=tempWord+", ";
  128.             tempWord="";
  129.         }
  130.     }
  131.     if(tempWord.find("the",0)!=string::npos)theList+=tempWord;
  132.     if(theList=="")cout<<"There are no words containing 'the'."<<endl;
  133.     else cout<<"Words containing 'the': "<<theList<<endl;
  134. }
  135. int main()
  136. {
  137.     string text;
  138.     cout<<"Please enter a proper sentence: ";
  139.     getline(cin,text);
  140.     cout<<"Total characters: "<<text.length()<<endl;
  141.     wordCounter(text);
  142.     wordLister(text);
  143.     textReverser(text);
  144.     upperCount(text);
  145.     lowerCount(text);
  146.     punctuationCounter(text);
  147.     middleWord(text);
  148.     longestWord(text);
  149.     shortestWord(text);
  150.     theList(text);
  151. }
Advertisement
Add Comment
Please, Sign In to add comment