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

Please be done in C. 2. You are to write a subroutine/function which will expect

ID: 3589589 • Letter: P

Question

Please be done in C.

2. You are to write a subroutine/function which will expect a string as input. The function should count the number of blocks of space characters plus one. the total number of words in the string calculated as The subroutine should then return this quantity as an integer. Two or more consecut characters should only count as one block. Example: The string "3ob has twelve apples. " shouid report 4 words because it conains three space characters. Note that the ASClI code for space is On20, Exampie: The string "Bob has twelve contains three blocks of space characters apples.” should also report 4 words because it

Explanation / Answer

#include<stdio.h>

main()
{
// taking user input
char st[1000];
printf("Enter a string: ");
gets(st);
  
// initializing count to 1
int count = 1, i;
  
// looping through each character in string
for(i=0;st[i];i++)
{
// if it is space incrementing count by 1
if(st[i]==' ')
count++;
  
// to ignore many spaces, looping thorugh all spaces
while(st[i]==' ')
i++;
}
  
// printing output
printf("Words = %d",count);
}

/* SAMPLE OUTPUT

Enter a string: Bob has twelve apples.
Words = 4

Enter a string: Bob has twelve apples
Words = 4


*/