In: Computer Science
6.40 LAB: Warm up: Text analyzer & modifier (1) Prompt the user to enter a string of their choosing. Output the string. (1 pt) Ex: Enter a 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. (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 '\t'. Call the OutputWithoutWhitespace() function in main(). (2 pts) Ex: Enter a 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.
In C++ Please
layout given
#include <iostream>
#include <string>
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;
}
//compiled using gcc compiler
#include <iostream>
#include <string>
using namespace std;
int GetNumOfCharacters(const string usrStr) {
int i;
for(i=0;usrStr[i]!='\0';i++); // number of characters
return i;
}
void OutputWithoutWhitespaces(string Str){
int count = 0;
char new_str[40];
// Traverse the given string. If current character
// is not space, then place it at index 'count++'
for (int i = 0; Str[i]!='\0'; i++)
if (Str[i] != ' ')
new_str[count++] = Str[i]; // here count is
// incremented
new_str[count] ='\0';
cout<<"\nString with no whitespaces:"<<new_str<<"\n";
}
int main() {
string usrStr;
int number_char;
cout<<"Enter a sentence or phrase:"; // user string input
getline(cin,usrStr);
cout<<"You entered:"<<usrStr;
number_char=GetNumOfCharacters(usrStr);
cout<<"\nNumber of charcters:"<<number_char;
OutputWithoutWhitespaces(usrStr);
return 0;
}
#OUTPUT