Question

COMPLETE IN C++ Declare and initialize a global constant named SIZE with the value 50. Write...

COMPLETE IN C++

  • Declare and initialize a global constant named SIZE with the value 50.

  • Write a void function called count that accepts two parameters-- a C-string called str representing a C-string passed to the function and an array of integers called alphabets to store the count of the letters of the C-string.

  • Note that the alphabets array stores 26 counters – one for each letter of the alphabet. Use the same counter for lowercase and uppercase characters. The first element is the counter for ‘A’ or ‘a’, the second element is the counter for ‘B’ or ‘b’ and so on.

  • Inside your function,
    o Using a loop of your choice, process one character at a time. o Test whether the character is an alphabet.

    • ▪ If it is an alphabet, convert the alphabet to uppercase and find its ascii value.

    • ▪ Increment the appropriate counter of the alphabets array.

• Subtract 65 from the ascii value of the character to obtain the index

of the array to increment the correct counter. • Inside your main function:

o Declare a C-string named str and use SIZE as the size of the C-string.
o Declare an array of integers called alphabets of size 26 and initialize all

values with 0.

o Using a suitable message, get the C-string from the user. The user may enter multiple words.

o Call function count and pass the appropriate arguments.
o Inside a loop of your choice, process each element of the alphabets array, one at

time.

▪ If the count is non-zero, display the corresponding uppercase characters as well as the counts.

• Due to time constraints, no comments are required in this code.

Homework Answers

Answer #1

For this program we have used a string function strlen to get the length of the string passed. Ascii value is obtained by using int() function. For characters which are in capital letters 65 is subtracted from their ascii value to get the corresponding position in alphabets array. For small letters, first it is coverted to capital letters by subtracting 32 from their ascii value and then 65 is subtracted to get corresponding position in the alphabets array. After getting corresponding position the value in the array is incremented by 1.

Program code and output screen is given for your reference.

#include <iostream>
#include<string.h>
using namespace std;
const int SIZE=50;

void count(char str[50],int alphabets[26])
{

int n,p,i;

n=strlen(str);

for (i=0;i<n;i++)
{
if(int(str[i]>=65 and int(str[i])<=90))
{
p=int(str[i])-65;
alphabets[p]=alphabets[p]+1;
}
else if (int(str[i]>=97 and int(str[i])<=122))
{
  
p=int(str[i])-32;
  
p=p-65;
alphabets[p]=alphabets[p]+1;
  
}
}
for(i=0;i<26;i++)
{
  
if (alphabets[i]!=0)
{
  
cout<<char(i+65)<<" "<<alphabets[i]<<endl;
}
}
}

int main()
{
char str[SIZE];
int alphabets[26],i;
for (i=0;i<26;i++)
{
alphabets[i]=0;
}
cout<<"Enter a string : ";
cin.getline(str,50);
cout<<endl;
  
count(str,alphabets);
  
  
return 0;
}

PROGRAM CODE



#include <iostream>
#include<string.h>
using namespace std;
const int SIZE=50;

void count(char str[50],int alphabets[26])
{
   
   int n,p,i;
   
   n=strlen(str);
   
   for (i=0;i<n;i++)
   {
       if(int(str[i]>=65 and int(str[i])<=90))
       {
          p=int(str[i])-65;
          alphabets[p]=alphabets[p]+1;
       }
       else if (int(str[i]>=97 and int(str[i])<=122))
       {
    
           p=int(str[i])-32;
    
           p=p-65;
           alphabets[p]=alphabets[p]+1;
    
       }
   }
   for(i=0;i<26;i++)
    {
    
        if (alphabets[i]!=0)
        {
    
            cout<<char(i+65)<<"   "<<alphabets[i]<<endl;
        }
    }
}

int main()
{
    char str[SIZE];
    int alphabets[26],i;
    for (i=0;i<26;i++)
    {
        alphabets[i]=0;
    }
    cout<<"Enter a string : ";
    cin.getline(str,50);
    cout<<endl;
    
    count(str,alphabets);
    
    
    return 0;
}

OUTPUT SCREEN

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
For questions 1 and 2 declare the array size as name constant first then use it...
For questions 1 and 2 declare the array size as name constant first then use it to declare the array 1. Declare an array named scores of type double and size 30. 2. Declare an array named names of string objects and size 15 3. Given the following array definition: int values[] = {2, 5, 8, 11}; What does each of the following display? cout << values[1];         B) cout << value[3]+values[0];   Define a three element array named letters, initialize it...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an array of ints, an int value named element, and an int value named end. Return a bool based on whether the element appears in the array starting from index 0 and up to but not including the end index. Generate Random Array Write a function that takes as parameters an array of integers and another integer for the size of the array. Create a...
IN C++ Write a program named printStringReverse.cpp to read in a C-Style sting. Print the string...
IN C++ Write a program named printStringReverse.cpp to read in a C-Style sting. Print the string forward and then backward using pointer increment and decrement. Declare the C-Style string as following: char s[50]; Read the string as follows; cin >> s; Write the following code to get the actual number of characters from your string. int count=0; while (s[count] != NULL) { count++; } count contains the actual length of your string since we declared the array to hold a...
1. Write a function named "countPositiveNegative" that accepts an array of integers, its size and return...
1. Write a function named "countPositiveNegative" that accepts an array of integers, its size and return the number of positive and negative in the array through the use of parameters. In addition, it also returns the number of 0s in the array as the return value. For example, array of {10, 20, -30, -40, 50, 0, 60} will return 4 for positives, 2 for negatives and 1 for 0's as the return value. 2. Write a function named "rotateRight" that...
C++ Write a function named timesOfLetter that reads an array and returns the number of times...
C++ Write a function named timesOfLetter that reads an array and returns the number of times of each lowercase vowel and each uppercase vowel appear in it using reference parameter. • Write a function named timesOfNumber that reads an array and returns the number of times of each odd number, and each even number appear in it using reference parameter. • Write a function named isChar() that determines the input is alphabetic or not during inputting. • Write a function...
C Program Write a program to count the frequency of each alphabet letter (A-Z a-z, total...
C Program Write a program to count the frequency of each alphabet letter (A-Z a-z, total 52 case sensitive) and five special characters (‘.’, ‘,’, ‘:’, ‘;’ and ‘!’) in all the .txt files under a given directory. The program should include a header count.h, alphabetcount.c to count the frequency of alphabet letters; and specialcharcount.c to count the frequency of special characters. Please only add code to where it says //ADDCODEHERE and keep function names the same. I have also...
Complete this in C++ and explain what is being done. 1      Introduction The functions in the...
Complete this in C++ and explain what is being done. 1      Introduction The functions in the following subsections can all go in one big file called pointerpractice.cpp. 1.1     Basics Write a function, int square 1(int∗ p), that takes a pointer to an int and returns the square of the int that it points to. Write a function, void square 2(int∗ p), that takes a pointer to an int and replaces that int (the one pointed to by p) with its...
Write a function in c using #include <stdio.h> that takes a one-dimensional integer array and returns...
Write a function in c using #include <stdio.h> that takes a one-dimensional integer array and returns the index of the first occurance of the smallest value in the array. Your function must be able to process all the elements in the array. Create a function prototype and function definition (after the main function). Your main function should declare a 100 element integer array. Prompt the user for the number of integers to enter and then prompt the user for each...
Note: Do not use classes or any variables of type string to complete this assignment Write...
Note: Do not use classes or any variables of type string to complete this assignment Write a program that reads in a sequence of characters entered by the user and terminated by a period ('.'). Your program should allow the user to enter multiple lines of input by pressing the enter key at the end of each line. The program should print out a frequency table, sorted in decreasing order by number of occurences, listing each letter that ocurred along...
In this lab, you complete a partially prewritten C++ program that uses an array. The program...
In this lab, you complete a partially prewritten C++ program that uses an array. The program prompts the user to interactively enter eight batting averages, which the program stores in an array. The program should then find the minimum and maximum batting average stored in the array as well as the average of the eight batting averages. The data file provided for this lab includes the input statement and some variable declarations. Comments are included in the file to help...