(1) Prompt the user to enter a string of their choosing. Output the string. (1 p
ID: 3827894 • Letter: #
Question
(1) Prompt the user to enter a string of their choosing. Output the string. (1 pt)
Ex:
(2) Complete the GetNumOfCharacters() function, which returns the number of characters in the user's string. We encourage you to use a for loop in this function. (2 pts)
(3) In main(), call the GetNumOfCharacters() function and then output the returned result. (1 pt)
(4) Implement the OutputWithoutWhitespace() function. OutputWithoutWhitespace() outputs the string's characters except for whitespace (spaces, tabs). Note: A tab is ' '. Call the OutputWithoutWhitespace() function in main(). (2 pts)
Ex:
c++
_________________________________________________________________________
#include
#include
using namespace std;
//Returns the number of characters in usrStr
int GetNumOfCharacters(const string usrStr) {
/* Type your code here. */
}
int main() {
/* Type your code here. */
return 0;
}
Explanation / Answer
#include <iostream>
#include <stdlib.h>
using namespace std;
//Returns the number of characters in usrStr
int GetNumOfCharacters(const string usrStr) {
/* Type your code here. */
int len=0;
len=usrStr.length();
//string s[]=usrStr.split(" ");
/*for(int i=0;i<s.length;i++)
{
len+=s[i].length();
}*/
return len;
}
void outputWithoutWhitespaces(string usrStr)
{
int i=0;
int j=0;
while(j!=usrStr.length())
{
if(usrStr[j]!=' ')
{usrStr[i]=usrStr[j];
i++;
}j++;
}
while(i!=usrStr.length())
usrStr[i++]='';
cout<<" String with no whitespace : "<<usrStr<<" ";
}
int main() {
/* Type your code here. */
cout<<"Enter the sentence or phrase: ";
//string str;
//cin>>str;
char data[1000];
cin.getline(data,1000);
cout<<" You Entered : "<<data;
cout<<" Number of Characters : "<<GetNumOfCharacters(data);
outputWithoutWhitespaces(data);
return 0;
}
//Output:
Enter the sentence or phrase: The only thing we have to fear is fear itself.
You Entered : The only thing we have to fear is fear itself.
Number of Characters : 46
String with no whitespace : Theonlythingwehavetofearisfearitself.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.