Question

C++ A Program for Examining String (1) Ask the customer to type any string they like...

C++

A Program for Examining String

(1) Ask the customer to type any string they like and then print it to the console.

Example:

Type a sentence:

The CIS161 Computer Science II course is so much fun!

 

You typed: The CIS161 Computer Science II course is so much fun!

(2) Write the remaining code for the CountOfLetters() function, which returns the number of characters in the string that was entered. To get full credit for this question, this should be implemented with the use of a for loop.

(3) The CountOfLetters() function should be called from main() and the value returned must be printed.

(4) Implement the PrintEverythingExceptWhitespaces() function.

PrintEverythingExceptWhitespaces() prints the string's characters except for whitespace (spaces, tabs). Note: A tab is '\t'. Call the PrintEverythingExceptWhitespaces() function in main().

Ex:

The CIS161 Computer Science II course is so much fun!
You typed: The CIS161 Computer Science II course is so much fun!
Number of characters: 53
String with no whitespace:

TheCIS161ComputerScienceIIcourseissomuchfun!

This is what we are given to work with:

#include <iostream>
#include <string>
using namespace std;

//Returns the number of characters in usrStr
int CountOfLetters(const string usrStr) {

/* Type your code here. */

}

int main() {

/* Type your code here. */

return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the code below.

#include <iostream>
#include <string>

using namespace std;

int CountOfLetters(const string usrStr) {

int count = 0;
for (int i=0;i<usrStr.length();i++) {
count++;
}

return(count);
}

void PrintEverythingExceptWhitespaces(string str){
int i=0,j=0,k=0;
string str1;
while (str[i])
{
   if(str[i]!=' ')
   str[j++]=str[i];
   i++;
}
   str[j]='\0';
   for(k=0;k<str.length()-8;k++)
cout << str[k];
}

int main()
{   
string str;
cout<<"Type a sentence:\n";
getline(cin, str);
cout << "You typed: " << str;
cout<<"\n";
cout<<"Number of characters: "<<CountOfLetters(str);
cout<<"\n";
cout<<"String with no whitespace :";
PrintEverythingExceptWhitespaces(str);
return 0;
}

Output:

Add a comment
Know the answer?
Add Answer to:
C++ A Program for Examining String (1) Ask the customer to type any string they like...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • // Write a program that determines how many of each type of vowel are in an...

    // Write a program that determines how many of each type of vowel are in an entered string of 50 characters or less. // The program should prompt the user for a string. // The program should then sequence through the string character by character (till it gets to the NULL character) and count how many of each type of vowel are in the string. // Vowels: a, e, i, o, u. // Output the entered string, how many of...

  • Here is the beginning of the code; #include <string> #include <iostream> int main() { std::string sentence;...

    Here is the beginning of the code; #include <string> #include <iostream> int main() { std::string sentence; std::getline(std::cin, sentence); // manipulate the sentence here std::cout << sentence << "\n"; }} Lab 4.5.1 Text manipulation: duplicate white space Objectives Familiarize the student with: • the basics of string and text manipulation. Scenario Write a program that will read a line of text and remove all duplicate whitespace. Note that any single whitespace characters must remain intact. #include <string> #include <iostream> int main()...

  • C++ help Format any numerical decimal values with 2 digits after the decimal point. Problem descr...

    C++ help Format any numerical decimal values with 2 digits after the decimal point. Problem description Write the following functions as prototyped below. Do not alter the function signatures. /// Returns a copy of the string with its first character capitalized and the rest lowercased. /// @example capitalize("hELLO wORLD") returns "Hello world" std::string capitalize(const std::string& str); /// Returns a copy of the string centered in a string of length 'width'. /// Padding is done using the specified 'fillchar' (default is...

  • C++ (1) Prompt the user to enter a string of their choosing (Hint: you will need...

    C++ (1) Prompt the user to enter a string of their choosing (Hint: you will need to call the getline() function to read a string consisting of white spaces.) Store the text in a string. Output the string. (1 pt) Ex: Enter a sample text: We'll continue our quest in space. There will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes and our journeys continue!...

  • In this question you have to write a C++ program to convert a date from one...

    In this question you have to write a C++ program to convert a date from one format to another. You have to write a complete program consisting of a main() function and a function called convertDate(). The function receives a string of characters representing a date in American format, for example December 29, 1953. The function has to convert this date to the international format. For example: If the string December 29, 1953 is received, the string that the function...

  • QUESTION 6 15 marks In this question you have to write a C++ program to convert...

    QUESTION 6 15 marks In this question you have to write a C++ program to convert a date from one format to another. You have to write a complete program consisting of a main() function and a function called convertDate(). The function receives a string of characters representing a date in American format, for example December 29, 1953. The function has to convert this date to the international format. For example: If the string December 29, 1953 is received, the...

  • C++ problem. hi heys, i am trying to remove beginning and the end whitespace in one...

    C++ problem. hi heys, i am trying to remove beginning and the end whitespace in one file, and output the result to another file. But i am confused about how to remove whitespace. i need to make a function to do it. It should use string, anyone can help me ? here is my code. #include <iostream> #include <iomanip> #include <cstdlib> #include <fstream> using namespace std; void IsFileName(string filename); int main(int argc, char const *argv[]) { string inputfileName = argv[1];...

  • Write a program that prompts the user to input a string. The program then uses the...

    Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. #include <iostream> #include <string> using namespace std; void removeVowels(string& str); bool isVowel(char ch);...

  • (1) Prompt the user to enter a string of their choosing. Store the text in a...

    (1) Prompt the user to enter a string of their choosing. Store the text in a string. Output the string. (1 pt) Ex: Enter a sample text: We'll continue our quest in space. There will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes and our journeys continue! You entered: We'll continue our quest in space. There will be more shuttle flights and more shuttle crews...

  • In C++ write a program that will read three strings from the user, a phrase, a...

    In C++ write a program that will read three strings from the user, a phrase, a letter to replace, and a replacement letter. The program will change the phrase by replacing each occurrence of a letter with a replacement letter. For example, if the phrase is Mississippi and the letter to replace is 's' the new phrase will be "miizzizzippi". in the function, replace the first parameter is a modifiable string, the second parameter is the occurrence of a letter...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT