Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C program: (1) Prompt the user to enter a string of their choosing. Output the s

ID: 3920709 • Letter: C

Question

C program:

(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:

Code to edit:

#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

#include int GetNumOfCharacters(char str[]) { int i; for(i = 0; str[i] != 0; i++) { } return i; } void OutputWithoutWhitespace(char str[]) { int i; for(i = 0; str[i] != 0; i++) { if(str[i] != ' ' && str[i] != ' ') { printf("%c", str[i]); } } printf(" "); } int main() { char str[1000]; printf("Enter a sentence or phrase: "); gets(str); printf("You entered:%s ", str); printf("Number of characters: %d ", GetNumOfCharacters(str)); printf("String with no whitespace: "); OutputWithoutWhitespace(str); return 0; }