Hi I have the following code for an assignment that reads li

Hi,

I have the following code for an assignment that reads lines then output the number of lines/words/and words per line. However, the instructor said we cannot use global variables, thus everything has to be in functions. Can someone help with converting it into function to avoid global variable declaration?

thanks,

#include <stdio.h>
#include <stdlib.h>

int main()
{
    //Array to hold words per line
    int wordsPerline[50];

    //Variables to hold individual counts
    int lines = 0, words = 0, totalWords = 0, i;

    char ch;

    printf(\"Enter the poem: \ \");

    //Reading input from user
    while((ch=getchar()) != \'.\')
    {
        //New line character
        if(ch==\'\ \')
        {
            //If there are no words
            if(words != 0)
                words++;

            //Updating total words
            totalWords += 1;

            //Storing value in array
            wordsPerline[lines] = words;

            //Incrementing number of lines
            lines ++;

            //Reset the word count
            words = 0;
        }

        //Spaces and tab
        if(ch ==\' \' || ch==\'\\t\')
        {
            //Updating total words
            totalWords += 1;

            //Updating words
            words ++;
        }
    }

    //Printing results
    if(totalWords == 1)
        printf(\"\ %d word on \", totalWords);
    else
        printf(\"\ %d words on \", totalWords);

    if(lines == 1)
        printf(\" %d line \ \ \", lines);
    else
        printf(\" %d lines \ \ \", lines);

    //Printing words per line
    for(i=0; i<lines; i++)
        printf(\" %d \", wordsPerline[i]);

    printf(\"\ \");
    return 0;
}

Solution

#include <stdio.h>
#include <stdlib.h>

void readF()
{
//Array to hold words per line
int wordsPerline[50];
//Variables to hold individual counts
int lines = 0, words = 0, totalWords = 0, i;
char ch;
printf(\"Enter the poem: \ \");
//Reading input from user
while((ch=getchar()) != \'.\')
{
//New line character
if(ch==\'\ \')
{
//If there are no words
if(words != 0)
words++;
//Updating total words
totalWords += 1;
//Storing value in array
wordsPerline[lines] = words;
//Incrementing number of lines
lines ++;
//Reset the word count
words = 0;
}
//Spaces and tab
if(ch ==\' \' || ch==\'\\t\')
{
//Updating total words
totalWords += 1;
//Updating words
words ++;
}
}
//Printing results
if(totalWords == 1)
printf(\"\ %d word on \", totalWords);
else
printf(\"\ %d words on \", totalWords);
if(lines == 1)
printf(\" %d line \ \ \", lines);
else
printf(\" %d lines \ \ \", lines);
//Printing words per line
for(i=0; i<lines; i++)
printf(\" %d \", wordsPerline[i]);
printf(\"\ \");
}
int main()
{
readF();
return 0;
}

=====================================

Output:

akshay@akshay-Inspiron-3537:~/Chegg$ gcc readfile.c
akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out
Enter the poem:
Hi i am john
how are you?
.

7 words on 2 lines

4 3

Hi, I have the following code for an assignment that reads lines then output the number of lines/words/and words per line. However, the instructor said we canno
Hi, I have the following code for an assignment that reads lines then output the number of lines/words/and words per line. However, the instructor said we canno
Hi, I have the following code for an assignment that reads lines then output the number of lines/words/and words per line. However, the instructor said we canno

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site