Question

C program: Write a program that assigns and counts the number of each alphabetic character in...

C program:

Write a program that assigns and counts the number of each alphabetic character in the Declaration of Independence and sorts the counts from the most used to the least used character.

  • Consider upper and lower case letters the same.
  • The frequency of each character should be accumulated in an array.
  • USE POINTERS to increment counts for each letter, sort your array, and display the results.
  • Output should consist of two columns: (1) each letter 'a'-'z' and (2) the frequency with which each letter occurs.

Submitting the Assignment

You should submit the following:

  • Your C source code.
  • A Word document that contains your program and a screen shot of your results..  

Grading will be based upon the following:

  • Program logic. Program produces results indicated in the problem statements. This includes
    • Use of an array to calculate frequencies.
    • Use of pointers to increment counts for each letter, sort the array to list characters from most to least, and display the results.
  • Program syntax. The C language is used correctly.(10 points).
  • Program appearance and formatting. White space and indentation are consistent with text examples. (10 points).
  • Program documentation. Comments are consistent with text examples. (5 points).
0 0
Add a comment Improve this question Transcribed image text
Answer #1

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

int main()
{
char c,str[100];
int i,t,j=0, len;
int *freq,*f;
char al[26];
//allocate memory for storing the frequencies
freq = (int *)malloc(26 * sizeof(int));
f = (int *)malloc(26 * sizeof(int));

/* Input string from user */
printf("Enter any string: ");
gets(str);
//find the length of the entered string
len = strlen(str);

// Initialize frequency of each character to 0
for(i=0; i<26; i++)
{
*(freq+i) = 0;
}


// Find total number of occurrences of each character
for(i=0; i<len; i++)
{
// If the current character is lowercase alphabet
if(str[i]>='a' && str[i]<='z')
{
*(freq + (str[i]-97))= *(freq + (str[i]-97))+1;
          
}
else if(str[i]>='A' && str[i]<='Z') // if the character is uppercase character
{
   *(freq + (str[i]-65))= *(freq + (str[i]-65)) +1;
  
}
}


for(i=0; i<26; i++)
{
// If current character exists in given string
if(*(freq+i) != 0)
{
al[j]=(char)(i+97); //assignthe character to al
*(f+j)=*(freq+i); //assign the frequencies to f
j++;
       }
}
free(freq); //delete the freq array from meory
len=j; //assign the total number of alphabet types to len
//sort the frequescies in descending order
for(i=0;i<len;i++)
for(j=0;j<len;j++)
{
   if(*(f+i)>*(f+j))
   {
      t=*(f+i); //swap the frequencies
      *(f+i)=*(f+j);
      *(f+j)=t;
     
      c=al[i]; //swap the characters
      al[i]=al[j];
      al[j]=c;
           }
       }
       /* Print the frequency of all characters in the string */
   printf("\nFrequency of all characters in the given string: \n");
   for(i=0;i<len;i++)  
printf("\n%c %d",al[i],*(f+i));

free(f); //deallocate f

return 0;
}

output

Add a comment
Know the answer?
Add Answer to:
C program: Write a program that assigns and counts the number of each alphabetic character in...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write in C. Simple Program (beginner) Assignment: Write a program Character Pointers and Functions. (like program...

    Write in C. Simple Program (beginner) Assignment: Write a program Character Pointers and Functions. (like program #5-5). Keyboard input to enter one character string. Using a single dimension array, populate the array with the character string, call a function using pointers to reverse order the character string, pass back to the main the output and total_count_of_characters. (maybe use a global variable for the total count). Print display the reversed char string and total_count. <END>

  • This is for C++ Write a program that reads in a sequence of characters entered by...

    This is for C++ 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 with the number of times it occured. All non-alphabetic characters must...

  • C Program In this assignment you'll write a program that encrypts the alphabetic letters in a...

    C Program In this assignment you'll write a program that encrypts the alphabetic letters in a file using the Vigenère cipher. Your program will take two command line parameters containing the names of the file storing the encryption key and the file to be encrypted. The program must generate output to the console (terminal) screen as specified below. Command Line Parameters Your program must compile and run from the command line. The program executable must be named “vigenere” (all lower...

  • Analyze two sorts in C++, the first being a bubble sort and the second being a...

    Analyze two sorts in C++, the first being a bubble sort and the second being a quick sort. Create a project for each sort. Use 4 data set sizes. Results will be based on execution time for each sort. The size should have a regular increment. For example, using a regular increment of 10,000, you might have sizes of 10,000, 20,000, 30,000, and 40,000. Choose your data size based upon the ability to obtain meaning results. Need: Two screen shots...

  • Using basic c++ write 2 separate codes for this assignment. Program #1 Write a program that...

    Using basic c++ write 2 separate codes for this assignment. Program #1 Write a program that calculates the average of a group of test scores, where the lowest score in the group is dropped. It should use the following functions. • void getScore() should ask the user for a test score, store it in the reference parameter variable, and validate it. This function should be called by the main once for each of the five scores to be entered. •...

  • Can someone help me write a C++ program such that: 1. It counts the number of...

    Can someone help me write a C++ program such that: 1. It counts the number of alphabetic characters in a text file; this mean it ignores any character that is not alphabetic 2. Prompts the user to write the name of the file 3. Count's the number of A's, B's, C's, ..and so on For the sake of this program, both capital 'A' and lowercase 'a' and treated as equal. An output would something like this "A's = 10 B's...

  • C Programming Language on Linux - Word Frequency Program Please write a Program in C that...

    C Programming Language on Linux - Word Frequency Program Please write a Program in C that will accept a text file name as a command-line argument via a main program that will do the following: First, read the file (first pass) and create a linked list of words (in their order of occurrence), with the frequency of each word set to 0. Then, read the file (second pass) and for each word identified, search the linked list, and when found,...

  • c++ no pointers just follow instructions Write a program (array.cpp) that read positive floating point numbers...

    c++ no pointers just follow instructions Write a program (array.cpp) that read positive floating point numbers into an array of capacity 100. The program will then ask for one of three letters(A, M or S). if the user inputs something other than one of these letters, then the program should ask for that input until an A, M, or S is entered. A do-while loop should be used for this. If the user inputs an A, then the program should...

  • In C Programming Language, write a program Character Pointers and Functions. Keyboard input to enter one...

    In C Programming Language, write a program Character Pointers and Functions. Keyboard input to enter one character string. Using a single dimension array, populate the array with the character string, call a function using pointers to reverse order the character string, pass back to the main the output and total_count_of_characters. (maybe use a global variable for the total count). Print display the reversed char string and total_count.

  • In C Programming Language, write a program Character Pointers and Functions. Keyboard input to enter one...

    In C Programming Language, write a program Character Pointers and Functions. Keyboard input to enter one character string. Using a single dimension array, populate the array with the character string, call a function using pointers to reverse order the character string, pass back to the main the output and total_count_of_characters. (maybe use a global variable for the total count). Print display the reversed char string and total_count.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT