(1) Prompt the user to enter a string of their choosing. Output the string. (1 p
ID: 3594244 • 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:
#include<stdio.h>
#include <string.h>
//Returns the number of characters in usrStr
int GetNumOfCharacters(const char usrStr[]) {
/* Type your code here. */
}
int main(void) {
/* Type your code here. */
return 0;
}
Explanation / Answer
*************************************************************************************************************
The sample input output is giver below:
Two methods are implemented as given in the question
1.GetNumOfCharacters(const char usrStr[])
2..OutputWithoutWhitespace(const char usrStr[])
The program is tested for various input and outputs.
Both the methods uses a while loop and iterates on the input char array until '' is encountered.
First method calculates the number of characters and the second method copies character to a new char array if the character is not a white space.
I hope this helps you. All the best.
*************************************************************************************************************
CODE:
#include<stdio.h>
#include <string.h>
//Returns the number of characters in usrStr
int GetNumOfCharacters(const char usrStr[]) {
int i=0;
while(usrStr[i]!=''){
i++;
}
i--;
return i;
}
//Returns the characters in usrStr without spaces
void OutputWithoutWhitespace(const char usrStr[]) {
char str[999999];
int t=0;
int i=0;
while(usrStr[i]!=''){
if(usrStr[i]!=' ')
str[t++]=usrStr[i];
i++;
}
str[t] = '';
printf("String with no whitespace: %s",str);
}
int main(void) {
/* Type your code here. */
char name[999999], ch;
int i = 0;
printf("Enter a sentence or phrase: ");
while(ch != ' ') // terminates if user hit enter
{
ch = getchar();
name[i] = ch;
i++;
}
name[i] = ''; // inserting null character at end
printf("You entered: %s", name);
int count = GetNumOfCharacters(name);
printf("Number of characters: %d ",count);
OutputWithoutWhitespace(name);
return 0;
}
/*
Sample input/output:
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.
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.