Question

In: Computer Science

Rewrite the following program so    that the counting of the a's and    total count...

Rewrite the following program so
   that the counting of the a's and
   total count are done be
   separate functions
  
   The code you turn in should have 2 functions
*/

#include<stdio.h>

int main(void){
  
   char input[81];               // Input string
   int a=0, total=0;           // counter variables              
  
   // Ask for an input string
   puts(" Input a string of up to 80 characters and\n"
   " I will tell you how many a\'s it contains \n"
   " as well as a total character count.\n\n");
  
   fputs(" ",stdout);               // just make things line up

   // read the input string
   fgets(input,80,stdin);

   while(input[total] != '\0'){                       // go through the string

       if(input[total] == 'a' || input[total] == 'A')   // count the number of a's
           a++;

       total++;                       // total character count
   }
  
   total--;                           // fgets reads the newline - we don't want to count that
  
   // output the results
   printf(" %d a\'s and %d total characters.\n",a,total);
  
   return 0;
}

Solutions

Expert Solution

We have to rewrite given program so that counting of a's and total count are done in different functions.

So we will create a function named count_a() which will take input string as parameter and return number of a's .

We will create another function named count_total() which will also take input string as parameter and return total number of characters present in string.

Modified program-

#include<stdio.h>

//function to find number of a in string
int count_a(char input[]){
    int i=0;
    int a=0;
    while(input[i] != '\0'){     //traverse whole string till we reach the end
        if(input[i]=='a' || input[i]=='A'){
            a++;               // if character is 'a' then we will increase the count of a
        }
        i++;                  //increment i to reach to next element of string
    }
    return a;                //return taotal number of a's in the string
}

//function to find total number of characters in string
int count_total(char input[]){
    int total=0;
    
    while(input[total] != '\0'){          //traverse whole string till we reach end of string
        total++;                          //total characters count
    }
    total--;                              //fgets reads the newline - we don't want to count that
    return total;                        //return total number of characters
}

int main(void){
  
   char input[81];               // Input string
   int a, total;           // counter variables              
  
   // Ask for an input string
   puts(" Input a string of up to 80 characters and\n"
   " I will tell you how many a\'s it contains \n"
   " as well as a total character count.\n\n");
  
   fputs(" ",stdout);               // just make things line up

   // read the input string
   fgets(input,80,stdin);

   a=count_a(input);                //calling the function to count a's
   
   total=count_total(input);        // calling the function to count total characters
  
   // output the results
   printf(" %d a\'s and %d total characters.\n",a,total);
  
   return 0;
}

Comments are added in the program for better understanding.


Related Solutions

Using c# rewrite/edit the following program so that it runs the simulation 10,000 times (play the...
Using c# rewrite/edit the following program so that it runs the simulation 10,000 times (play the games 10,000 times) and count the number wins. Then calculate the winning probability by using the formula: the expected winning probability = the number of wins / 10,000 using System; class lottery { static void Main() { int n, random, choice = 1; Random randnum = new Random();    while (choice == 1) {    Console.Write("\nEnter a integer from 1 to 5:"); n =...
Rewrite Program to store the pairs of states and capitals so that the questions are displayed...
Rewrite Program to store the pairs of states and capitals so that the questions are displayed randomly. import java.util.*; public class quiz { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String arr[][]= {{"Alabama","Montgomery"},{"Alaska","Juneau"},{"Arizona","Phoenix"}}; int n =arr.length; int count=0; for(int i=0;i<n;i++) { System.out.printf("What is the capital of %s? ",arr[i][0]); String capital=sc.next(); if (arr[i][1].equalsIgnoreCase(capital)) { count++; System.out.println("Your answer is correct"); } else { System.out.printf("The correct answer should be %s\n",arr[i][1]); } } System.out.printf("The correct count is %d",count); } }
Exercise: Advanced counting Complete the program below in the answer box by entering functions so the...
Exercise: Advanced counting Complete the program below in the answer box by entering functions so the program can modify the value variable either by adding 1, adding 5, subtracting 1, or subtracting 5. There should only be one function updating the value. The functions subtract_one, add_one, etc. are helper functions as described above; they should simply call the value update function. from tkinter import * from tkinter.ttk import * # Write your functions here def main(): """Set up the GUI...
Write a program that reads a text file and reports the total count of words of...
Write a program that reads a text file and reports the total count of words of each length. A word is defined as any contiguous set of alphanumeric characters, including symbols. For example, in the current sentence there are 10 words. The filename should be given at the command line as an argument. The file should be read one word at a time. A count should be kept for how many words have a given length. For example, the word...
The League with DMA Rewrite your League program from Assignment 8 so that it uses Dynamic...
The League with DMA Rewrite your League program from Assignment 8 so that it uses Dynamic Memory Allocation (DMA) to create the team names and scores arrays. This is a good test of the modularity of your program. You will only need to make slight modifications to your main() function if you wrote your original program using functions similar to the following: void initializeData(string names[], int wins[], int size) void sort(string names[], int wins[], int size) void display(string names[], int...
In this assignment, you will use a LOC counting tool to count the lines of code...
In this assignment, you will use a LOC counting tool to count the lines of code of the tool itself. 1. There are several open source free tools for LOC counting such as CLOC and SLOC. 2. Run the tool to count the lines of code of the source files of the tool (usually in the src directory). 3. Submit a report which includes: a. Description of the tool 1. Name 2. Creator 3. Language(s) it is written 4. URL...
Having trouble with this assignment: Rewrite your most recent high scores program (shown below) so that...
Having trouble with this assignment: Rewrite your most recent high scores program (shown below) so that each name/score pair is stored in a struct named highscore. Except as noted below, this new program will continue to meet all of the requirements of your most recent high scores program. Your new program should meet the following requirements: The highscore struct should have two fields: an int named score and a char array named name. The char array should have 24 elements,...
Write one Java program and satisfy the following requirements: Rewrite the following if -else if -...
Write one Java program and satisfy the following requirements: Rewrite the following if -else if - else statement as an equivalent switch statement.   if (letter == 'A' | | letter == 'a')         System.out.println("Excellent");         else if (letter == 'B' | | letter == 'b')                System.out.println("You can do better");         else if (letter == 'C' | | letter == 'c')                System.out.println("Try harder");         else if (letter == 'D' | | letter == 'd')                System.out.println("Try much harder");        ...
C LANGUAGE ONLY Write a C program to count the total number of duplicate elements in...
C LANGUAGE ONLY Write a C program to count the total number of duplicate elements in an array. Enter the number of elements to be stored in the array: 3 Input 3 elements in the arrangement: element [0]: 5 element [1]: 1 element [2]: 1 Expected output: The total number of duplicate elements found in the array is: 1
2. [50] Write a C program to count the total number of commented characters and words...
2. [50] Write a C program to count the total number of commented characters and words in a C file taking both types of C file comments (single line and block) into account.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT