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...
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...
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...
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...
STRICT DOWNVOTE IF NOT DONE FULLY, WILL REPORT ALSO IF COPY PASTED OR MODIFIED ANSWER Develop...
STRICT DOWNVOTE IF NOT DONE FULLY, WILL REPORT ALSO IF COPY PASTED OR MODIFIED ANSWER Develop a class, using templates, to provide functionality for a set of recursive functions. The functions specified as recursive must be written recursively (not iterativly). The UML class specifications are provided below. A main will be provided. Additionally, a make file will need to be developed and submitted. ● Recursion Set Class The recursion set template class will implement the template functions. recursionSet -length: int...
Task 2: Compare strings. Write a function compare_strings() that takes pointers to two strings as inputs...
Task 2: Compare strings. Write a function compare_strings() that takes pointers to two strings as inputs and compares the character by character. If the two strings are exactly same it returns 0, otherwise it returns the difference between the first two dissimilar characters. You are not allowed to use built-in functions (other than strlen()) for this task. The function prototype is given below: int compare_strings(char * str1, char * str2); Task 3: Test if a string is subset of another...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g,...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g, char wordlist[][MAX_WORD_LENGTH], int numwords)] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) What int setup_game needs to do setup_game() does exactly what the name suggests. It sets up a new game of hangman. This means that it picks a random word from the supplied wordlist array and...
Consider the C program (twoupdate) to demonstrate race condition. In this assignment, we will implement Peterson's...
Consider the C program (twoupdate) to demonstrate race condition. In this assignment, we will implement Peterson's algorithm to ensure mutual exclusion in the respective critical sections of the two processes, and thereby eliminate the race condition. In order to implement Peterson's Algorithm, the two processes should share a boolean array calledflagwith two components and an integer variable called turn, all initialized suitably. We will create and access these shared variables using UNIX system calls relating to shared memory – shmget,...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT