Guest User

Untitled

a guest
Feb 4th, 2016
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.02 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. void printNumCharacters     (const std::string& sentence); //Prints number of characters.
  5. void printNumWords          (const std::string& sentence); //Prints number of words.
  6. void printToUpper           (const std::string& sentence); //Prints sentence to upper case.
  7. void printSubString         (const std::string& sentence); //Prints a sub string.
  8.  
  9. int main()
  10. {
  11.     std::string sentence;
  12.  
  13.     std::cout << "Please enter a sentence. \n";
  14.     std::getline(std::cin, sentence);
  15.  
  16.     printNumCharacters  (sentence);
  17.     printNumWords       (sentence);
  18.     printToUpper        (sentence);
  19.     printSubString      (sentence);
  20.  
  21.  
  22.     return 0;
  23. }
  24.  
  25. void printNumCharacters(const std::string& sentence)
  26. {
  27.     std::cout << "Number of characters: " << sentence.length() << std::endl;
  28. }
  29.  
  30. void printNumWords(const std::string& sentence)
  31. {
  32.     char letter;
  33.     int words;
  34.  
  35.     unsigned int i = 0;
  36.     while(std::isspace(sentence.at(i))) { //Count spaces at start of sentence if any
  37.         i++;
  38.     }
  39.  
  40.     sentence.empty() ? words = 0 : words = 1;   //Ternary operator! We need to do this as the following loop counts the spaces, rather than the words
  41.                                                 //If the string is empty, then words = 0 : If not, then count the first word
  42.  
  43.     for (; i < sentence.length(); i++) //Simple "for loop" to iterate through each character of the string
  44.     {
  45.         letter = sentence[i];
  46.         if( letter == ' ')
  47.         {
  48.             words++;
  49.         }
  50.  
  51.     }
  52.  
  53.     std::cout << "Number of words: " << words << std::endl;
  54. }
  55.  
  56. void printToUpper(const std::string& sentence)
  57. {
  58.     std::locale loc;
  59.     for (std::string::size_type i = 0 ; i< sentence.length(); i++)
  60.     {
  61.         std::cout << std::toupper(sentence[i], loc); //I literally Googled this one lol
  62.     }
  63.     std::cout << "\n";
  64. }
  65.  
  66. void printSubString(const std::string& sentence)
  67. {
  68.     unsigned  start = 0;
  69.     unsigned int end = sentence.length() + 1;
  70.  
  71.  
  72.  
  73.     std::cout << "Enter where you want to start printing the string (Character number) between 0 and " << sentence.length() << std::endl;
  74.     do { //"Do while loop"
  75.  
  76.         std::cin >> start;
  77.         if( start < 0) std::cout << "Too small! Enter number greater than 0!" << std::endl;
  78.         if( start > sentence.length()) std::cout << "Too big! Enter number less than " << sentence.length() << "!" << std::endl;
  79.     } while ( start < 0 || start > sentence.length() );
  80.  
  81.     std::cout << "Enter where you want to end printing the string (Character number) between " << start << " and " << sentence.length() << std::endl;
  82.     do { //"Do while loop"
  83.  
  84.         std::cin >> end;
  85.         if( start < start) std::cout << "Too small! Enter number greater than 0!" << start << "!" << std::endl;
  86.         if( start > sentence.length()) std::cout << "Too big! Enter number less than " << sentence.length() << "!" << std::endl;
  87.     } while ( end < start || end > sentence.length() );
  88.  
  89.     std::cout << sentence.substr(start, end); //Built in!
  90. }
Advertisement
Add Comment
Please, Sign In to add comment