I am taking an intro to C class so I am a noviceto say the least. My assignment
ID: 3612868 • Letter: I
Question
I am taking an intro to C class so I am a noviceto say the least. My assignment is to create a wordcounter in C. It must be able to read threelines of text input by a user, store them as strings, andcount the number of words entered using strtok. Also, this program needs to have a function in addition tothe main function.This is what I have brought the codeto at this point in time. It seems to be ignoring the systempause though.
#include <stdio.h>
#include <string.h>
/* Main function performs the followingitems: Reads 3 lines of user generated text, counts thewords, outputs word count */
int main()
{
char text[ 3 ][ 100000 ],*tokenPtr; /* Creates 3 arrayseach of length 100,000 as well as the Pointer */
int i; /* int i will serve as theline counter */
int counter; /* int counter serves to count to total numberof words */
counter = 0; /* Setting word counter to 0 */
printf( "The followingprogram will count the total number of words used in 3 lines oftext. Enter 3 lines of text: " );
/*During the lines being less thanthree increase i by one and execute */
for ( i = 0; i <= 2; i++) {
gets( &text[i ][ 0 ] );
tokenPtr =strtok( &text[ i ][ 0 ], " " );
while (tokenPtr ) {
++counter;
tokenPtr = strtok(NULL, " " );
}
}
returncounter; /* Function returns the total number of wordscounted */
}
/*Print function takes the outputted word countfrom Main and prints the totals number of words*/
int print ()
{
int words =main(); /* Setsint words as the value returned from main */
printf( " Based on the textyou entered, the number of words you used is %d ", words );
system("pause");
return 0; /* End of function */
}
Explanation / Answer
please rate - thanks all explanations are comments #include #include #include //needed for getch() intprint(int); //prototype for function /* Main function performs the followingitems: Reads 3 lines of user generated text, counts thewords, outputs word count */ int main() { char text[ 3 ][100000 ], *tokenPtr; /*Creates 3 arrays each of length 100,000 as well as the Pointer*/ inti; /* int i will serve as the line counter */ intcounter; /* int counter serves to count to total number of words */ counter =0; /* Setting word counter to 0 */ printf( "Thefollowing program will count the total number of words used in 3lines of text. Enter 3 lines of text: " ); /*During the lines being lessthan three increase i by one and execute */ for ( i = 0; iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.