C++ Programming

  1. Write a program to print Hello, World on the standard output.
  2.  Write a program that uses the multiplication operator, *, to print the product.
  3. Program to use
    a separate statement to print each operand.
  4. Write a program that uses a while to sum the numbers from 50 to 100.
  5. write a while that prints the numbers from ten down to zero.
  6. Write a program that prompts the user for two integers. Print each number in the range specified by those two integers.
  7. What are the differences between int, long, long long, and short?
  8. To calculate a mortgage payment, what types would you use for the rate, principal, and payment? Explain why you selected each type.
  9. Determine the type of each of the following literals. Explain the differences among the literals in each of the four examples:
  10. What, if any, are the differences between the following definitions: int month = 9, day = 7; int month = 09, day = 07;
  11. What values do these literals represent? What type does each have? (a) “Who goes with F\145rgus?\012” (b) 3.14e1L (c) 1024f (d) 3.14L
  12. Using escape sequences, write a program to print 2M followed by a newline. Modify the program to print 2, then a tab, then an M, followed by a newline.
  13. Explain the following definitions. For those that are illegal, explain what’s wrong and how to correct it.
    (a) std::cin >> int input_value;
    (b) int i = { 3.14 };
    (c) double salary = wage = 9999.99;
    (d) int i = 3.14;
  14. Write a program to calculate the highest two numbers in an array with length n, and print these numbers with their indexes.
  15. Write a C++ program to change every letter in a given string with the letter following it in the alphabet (i.e. a becomes b, p becomes q, z becomes a).
  16. Write a C++ program to find the largest word in a given string.
  17. Write a C++ program to count all the characters, blank spaces and words in a given string.
  18. Write a C++ program to find a word in a given string that has the highest number repeated letters.

Write a C++ program to count all the characters, blank spaces and words in a given string.

Method 1

#include <iostream>
#include <string>

using namespace std;

int main() {
  string str;
  //Give the initials
  int characterCount = 0, wordCount = 0, blankSpaceCount = 0;

  cout << "Enter any sequence of words or line: ";
  getline(cin, str); 

  for (char c : str) {
  characterCount++; 
  if (c == ' ') {
      blankSpaceCount++;
    }
  }
  //Words should be separated by single space
  wordCount = blankSpaceCount + 1; 

  cout << "Number of characters: " << characterCount << endl;
  cout << "Number of words: " << wordCount << endl;
  cout << "Number of blank spaces: " << blankSpaceCount << endl;

  return 0;
}

Output

Enter any sequence of words or line: Coddesire is for programming and technology
Number of characters: 43
Number of words: 6
Number of blank spaces: 5

Method 2

#include <iostream>
#include <string>

using namespace std;

//Function to count character words and space
void count(const std::string& input, int& charCount, int& wordCount, int& spaceCount) {
    
    bool inWord = false;
    for (char ch : input) {
//Words should be separated by a single space
        if (ch != ' ') {
            charCount++; 
            if (!inWord) {
            wordCount++; 
            inWord = true;
        }
        } else {
            spaceCount++; 
            inWord = false; 
        }
    }
}

int main() {
    string input;
    cout << "Enter any sequence of words or line: ";
    getline(cin, input);

    //Give the initials
    int charCount = 0, wordCount = 0, spaceCount = 0;

    count(input, charCount, wordCount, spaceCount);

    cout << "Number of characters : " << charCount <<endl;
    cout << "Number of words: " << wordCount <<endl;
    cout << "Number of spaces: " << spaceCount <<endl;

    return 0;
}

Output

Enter any sequence of words or line: CodDesire is for programming and technology
Number of characters : 38
Number of words: 6
Number of spaces: 5
Subscribe
Notify of
guest


0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments