Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <windows.h>
- #include <math.h>
- #include <vector>
- using namespace std;
- //major credit goes to xX0T1Xx
- //function to count number of characters
- int characters(string text)
- {
- return text.length();
- }
- //function to count number of words
- int words(string text)
- {
- int words = 0;
- if(text[0]>0&&text[0]!=' ')words++;
- for (int c=0;c<text.length();c++)if(text[c]==' '&&text[c+1]!=' '&&text[c+1]!=NULL)words++;
- return words;
- }
- //function to list each words
- string eachWords(string text)
- {
- string output;
- for (int c=0;c<text.length();c++)
- {
- if(text[c]!=' ')
- {
- output+=text[c];
- } else if (text[c]==' '&&text[c+1]!=' ')
- {
- output+=", ";
- }
- }
- return output;
- }
- //function to reverse the text
- string rvs(string text)
- {
- string newText;
- for (int c=text.length()-1;c>-1;c--)newText+=text[c];
- return newText;
- }
- //function to count uppercase letters
- int uppercaseLetters(string text)
- {
- //65-90 (ASCII value for uppercase letters)
- int capitalLetters = 0;
- for (int c=0;c<text.length();c++)if((int)text[c]>=65&&(int)text[c]<=90)capitalLetters++;
- return capitalLetters;
- }
- //function to count lowercase letters
- int lowercaseLetters(string text)
- {
- //97-122(ASCII value for lowercase letters)
- int lowercaseLetters = 0;
- for (int c=0;c<text.length();c++)if((int)text[c]>=97&&(int)text[c]<=122)lowercaseLetters++;
- return lowercaseLetters;
- }
- //function to count the number of punctuation and symbols
- int punctuation(string text)
- {
- string chars = "~!@#$%^&*()_+|}{:'?<>`-=\][;,./";
- int punctuationChars = 0;
- for(int c=0;c<text.length();c++)
- {
- if(text[c]=='"')
- {
- punctuationChars++;
- } else {
- for(int k=0;k<chars.length();k++)if(text[c]==chars[k])punctuationChars++;
- }
- }
- return punctuationChars;
- }
- //ridiculously long function to find the middle word(s)
- string middleWord(string text)
- {
- int midWord = words(text)/2;
- if (words(text)%2==1)midWord++;
- string midsWord;
- int currentWord = 1;
- bool hasHitLetter=false;
- for (int c=0;c<text.length();c++)
- {
- if(text[c]!=' ')hasHitLetter=true;
- if (words(text)%2==1)if(currentWord==midWord&&text[c]!=' ')midsWord+=text[c];
- if (words(text)%2==NULL)if(currentWord==midWord||currentWord==midWord+1)midsWord+=text[c];
- if(text[c]==' '&&text[c+1]!=' '&&text[c+1]!=NULL&&hasHitLetter==true)currentWord++;
- }
- return midsWord;
- }
- //function to find the longest word
- string longWord(string text)
- {
- string tempWord="";
- string longestWord="";
- for(int lw=0;lw<text.length();lw++)
- {
- if(text[lw]!=' ')
- {
- tempWord+=text[lw];
- }
- else
- {
- tempWord="";
- }
- if(tempWord.length()>longestWord.length())
- {
- longestWord=tempWord;
- }
- }
- return longestWord;
- }
- //function to find the shortest word
- string shortWord(string text)
- {
- vector<string> words;
- for (int c=0;c<text.length();c++)if(text[c]!=' ')if(text[c+1]==' '||text[c+1]==NULL)words.push_back("");
- int wordCount = words.size();
- if(wordCount>0)
- {
- int currentWord=0;
- for (int c=0;c<text.length();c++)
- {
- if (text[c]==' '&&text[c+1]!=' ')currentWord++;
- if (text[c]!=' ')words[currentWord]+=text[c];
- }
- } else return NULL;
- int smallWordLen = text.length();
- string smallestWord;
- for (int w=0;w<wordCount;w++)
- {
- if (words[w].length()<smallWordLen)
- {
- smallestWord=words[w];
- smallWordLen=words[w].length();
- }
- }
- return smallestWord;
- }
- //function to find words containing 'the'
- string theCount(string text)
- {
- vector<string> words;
- int currentWord = 0;
- words.push_back("");
- for (int c=0;c<text.length();c++)
- {
- if(text[c]==' '&&text[c+1]!=' ')
- {
- currentWord++;
- words.push_back("");
- }
- words[currentWord]+=text[c];
- }
- vector<string> words2;
- int currentWord2 = 0;
- string currentWord3;
- for (int w=0;w<words.size();w++)
- {
- words2.push_back("");
- currentWord3 = words[w];
- for(int wc=0;wc<currentWord3.length();wc++)
- {
- if(currentWord3[wc]!=' ')
- words2[currentWord2]+=currentWord3[wc];
- }
- currentWord2++;
- }
- vector<string> theWords;
- int currentWord4 = 0;
- string currentWord5;
- bool hasAdded = false;
- int theWordsCount = 0;
- for (int word=0;word<words2.size();word++)
- {
- currentWord5 = words2[word];
- for (int c=0;c<currentWord5.length();c++)
- {
- if (currentWord5[c]=='t'&¤tWord5[c+1]=='h'&¤tWord5[c+2]=='e'&&hasAdded==false)
- {
- theWords.push_back(currentWord5);
- hasAdded = true;
- }
- }
- hasAdded = false;
- currentWord4++;
- }
- string returnData;
- for (int theword=0;theword<theWords.size();theword++)
- {
- if (theword!=theWords.size()-1)
- {
- returnData+=theWords[theword]+", ";
- } else {
- returnData+=theWords[theword];
- }
- }
- return returnData;
- }
- int main()
- {
- string sentence;
- cout<<"Enter a sentence: ";
- getline(cin, sentence);
- cout<<"Total Characters (Including Spaces): "<<characters(sentence)<<endl;
- cout<<"Total Word Count: "<<words(sentence)<<endl;
- cout<<"Words: "<<eachWords(sentence)<<endl;
- cout<<"Sentence Reversed: "<<rvs(sentence)<<endl;
- cout<<"Number of Capital Letters: "<<uppercaseLetters(sentence)<<endl;
- cout<<"Number of Lowercase Letters: "<<lowercaseLetters(sentence)<<endl;
- cout<<"Number of Punctuation Marks / Symbols: "<<punctuation(sentence)<<endl;
- cout<<"Middle Word(s): "<<middleWord(sentence)<<endl;
- cout<<"Longest word: "<<longWord(sentence)<<" ("<<longWord(sentence).length()<<" character(s) long)"<<endl;
- cout<<"Shortest word: ";
- if (words(sentence)==1)
- {cout<<sentence<<" ("<<sentence.length()<<" character(s) long)"<<endl;}
- else
- {cout<<shortWord(sentence)<<" ("<<shortWord(sentence).length()<<" character(s) long)"<<endl;}
- cout<<"Words containing 'the': "<<theCount(sentence)<<endl;
- system("pause>nul");
- return 0;
- }
Add Comment
Please, Sign In to add comment