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

Consider the C library function This function stores the starting address of the

ID: 3869911 • Letter: C

Question

Consider the C library function

This function stores the starting address of the next token to parse in a static local variable. The objective of this assignment is to appreciate the issues associated with using static local variables.

Write a C program to determine the average number of words per line by using strtok. You will write a function that does the following:

1. tokenizes a string containing multiple sentences into individual sentences (using strtok);

2. calls another function that returns the number of words in a sentence by tokenizing the string containing a sentence into individual words; and

3. calculates the average number of words per sentence.

Test your functions on the preamble of the U.S.

Explanation / Answer

#include<stdio.h>

#include<string.h>

int main() {

char str[];

scanf("%s", str);

char punctuationList[] =" .,;:!?'"";   

char *stringCopy;

char *word;

int numberOfWords, numberOfCharacters;

float averageLength;   

printf("Given string: %s ", str);   

puts(" Number of words in a given string:");   

numberOfWords = numberOfCharacters = 0;   

stringCopy = strdup(str);

//get the pointer to the first word

word = strtok(stringCopy, punctuationList);

while (word != NULL) {

numberOfWords++;

numberOfCharacters += strlen(word);   

printf("%s ", word);

word = strtok(NULL, punctuationList);

}   

averageLength = (float) numberOfCharacters / numberOfWords;

printf(" Total number of words: %d ", numberOfWords);   

printf("Average word length: %4.lf ", averageLength);   

free(stringCopy);

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote