Question

Need help in C

(a) Write a C program to read in a line of text and count the occurrence of each English alphabet. The lowercase version of a

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please let me know if you need further clarification on this.

(a).

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

int main(){
/*str is char array of size 200*/
char str[200];
/*to store the frequencies of each characters irrespective of small or caps*/
int frequency[26];

/*frequencies are initialized to 0*/
for(int i=0;i<26;i++){
frequency[i]=0;
}
printf("Enter a line of text: ");
scanf("%[^\t\n]s",str);

/*logic applied here, increase the frequency at i based on the character occur in the
string str. */
for(int i=0;i<strlen(str);i++){
if(str[i]>='A'&& str[i]<='Z'){
int ii=str[i]-'A';
frequency[ii]+=1;
}
if (str[i] >= 'a' && str[i] <= 'z') {
int ii = str[i] - 'a';
frequency[ii] += 1;
}
}
printf("\nLetter\tFrequency\n");
for(char i='A';i<='Z';i++){
printf("%c\t",i);
int k=i-'A';
for(int m=0;m<frequency[k];m++){
printf("*");
}
printf("\n");
}
  
return 0;
}


Sample input and output:

Enter a line of text: AAAAABBBBccccaaaDEFGXXXYYYzzz
Letter   Frequency
A   ********
B   ****
C   ****
D   *
E   *
F   *
G   *
H  
I  
J  
K  
L  
M  
N  
O  
P  
Q  
R  
S  
T  
U  
V  
W  
X   ***
Y   ***
Z   ***

#include #include #include <stdio.h> <string.h> math.h> 4 #include <stdlib.h> 6 int main) str is char array of size 200 charEnter a line of text : AAAAABBBBccc ca a aDEEGKXNYYzzz Letter Frequency

To run the same program in Dev-C++: In dev c++ for loop do not allow to declare the the variables and initialize in it for().So to run the same program in devC++, look below changes made.

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

int main(){
/*str is char array of size 200*/
char str[200];
/*to store the frequencies of each characters irrespective of small or caps*/
int frequency[26];

/*frequencies are initialized to 0*/
int i=0;
for(i=0;i<26;i++){
   frequency[i]=0;
}
printf("Enter a line of text: ");
scanf("%[^\t\n]s",str);

/*logic applied here, increase the frequency at i based on the character occur in the
string str. */
for(i=0;i<strlen(str);i++){
   if(str[i]>='A'&& str[i]<='Z'){
       int ii=str[i]-'A';
       frequency[ii]+=1;
   }
if (str[i] >= 'a' && str[i] <= 'z') {
   int ii = str[i] - 'a';
   frequency[ii] += 1;
   }
}

char iii;
int m;
printf("\nLetter\tFrequency\n");

for(iii='A';iii<='Z';iii++){
   printf("%c\t",iii);
   int k=iii-'A';
   for(m=0;m<frequency[k];m++){
           printf("*");
   }
   printf("\n");
}
  
return 0;
}

CAUsersraviblDocumentslmy.exe Enter a line of text: AAAAABBBBccccaaaDEFGXXXYYYzzz Letter Frequency pl ar ut ut om Process exiCAUsers ravib\Documentslmy.c [Executing] - Dev-C++5.11 e Edit earch View Project Execute Iools AStyle Window Help 凹 (globals)39 for(m-mfrequency[k]im+) 40 printf( 41 42 printf(In) 43 45 return 0; 46

(b):

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

int main(){
float temp[30]={15.5,18.0,17.6,12.3,15.4,15.2,13.5,14.2};
float*ptr;
float*copy=temp;

ptr =&temp[2];
printf("%f\n",temp[3]);
printf("%d\n", ptr);
printf("**%d\n",&temp[5]);/*----The address of the element index 5 of the array temp */
printf("%d\n",copy[5]);/*----The value or content element index 5 of the array pointer copy*/
printf("%d\n",ptr+1); /*----The address of the element index 3 of the array temp*/
printf("%d\n",sizeof(temp[0]));/*----The size of the value or content element index 0 of the array temp*/
printf("%d\n", sizeof(copy));/*---Thie size of the value or content element of the array temp pointed by pointer copy.*/

return 0;
}

Answer:

printf("**%d\n",&temp[5]);/*----The address of the element index 5 of the array temp */
printf("%d\n",copy[5]);/*----The value or content element index 5 of the array pointer copy*/
printf("%d\n",ptr+1); /*----The address of the element index 3 of the array temp*/
printf("%d\n",sizeof(temp[0]));/*----The size of the value or content element index 0 of the array temp*/
printf("%d\n", sizeof(copy));/*---Thie size of the value or content element of the array temp pointed by pointer copy.*/

please rate the solution, your rating is precious :)

Add a comment
Know the answer?
Add Answer to:
Need help in C (a) Write a C program to read in a line of text...
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
  • I need eclipse code for : Write a program that analyzes text written in the console...

    I need eclipse code for : Write a program that analyzes text written in the console by counting the number of times each of the 26 letters in the alphabet occurs. Uppercase and lowercase letters should be counted together (for example, both ‘A’ and ‘a’ should count as an A). Any characters that are not letters should be ignored. You must prompt the user to enter the text to be analyzed. Then, for any letter that appeared at least once...

  • I need help with the following question Write a script based on the program in Exercise...

    I need help with the following question Write a script based on the program in Exercise 11.13 that inputs several lines of text and uses String method indexOf to determine the total number of occurrences of each letter of the alphabet in the text. Uppercase and lowercase letters should be counted together. Store the totals for each letter in an array, and print the values in tabular format in an HTML5 textarea after the totals have been determined. I need...

  • Write a simple program in C++ that reads a line of text, changes each uppercase letter to lowerca...

    Write a simple program in C++ that reads a line of text, changes each uppercase letter to lowercase, and places each letter both in a queue and onto a stack. The program should then verify whether the line of text is a palindrome (a set of letters or numbers that is the same whether read forward or backward). DO NOT USE THE STACK LIBRARY, QUEUE LIBRARY, OR ANY OTHER LIBRARIES NOT COVERED YET.

  • Write a program that reads a line of text, changes each uppercase letter to lowercase, and places...

    Write a program that reads a line of text, changes each uppercase letter to lowercase, and places each letter both in a queue and onto a stack. The program should then verify whether the line of text is a palindrome (a set of letters or numbers that is the same whether read forward or backward). please use c++ language and use own queue class or class provided. plese do not use queue library. please use functions.

  • Question 2 Write a program that will read in a line of text up to 100...

    Question 2 Write a program that will read in a line of text up to 100 characters as string, and output the number of words in the line and the number of occurrences of each letter. Define a word to be any string of letters that is delimited at each end by whitespace, a period, a comma, or the beginning or end of the line. You can assume that the input consists entirely of letters, whitespace, commas, an<d periods. When...

  • 4. [8] Write a C function with prototype void letter_freq(const char wordll, int freaq ); This...

    4. [8] Write a C function with prototype void letter_freq(const char wordll, int freaq ); This function computes the number of appearances of each letter in the string word and stores them irn array freq of size 26. The letters are the 26 letters of the Latin alphabet whose ASCII values are in the range 97-122 for the lower case letters, and in the range 65-90 for the uppercase letters. You must account for uppercase and lowercase letter variants, which...

  • u also need to store the letters' ASCII number in the freq array 4. [8] Write...

    u also need to store the letters' ASCII number in the freq array 4. [8] Write a C function with prototype · void letter_freq(const char word[], int freq []); This function computes the number of appearances of each letter in the string word and stores them in array freq of size 26. The letters are the 26 letters of the Latin alphabet whose ASCII values are in the range 97-122 for the lower case letters, and in the range 65-90...

  • Need help for C program. Thx #include <stdio.h> #include <string.h> #include <ctype.h> // READ BEFORE YOU...

    Need help for C program. Thx #include <stdio.h> #include <string.h> #include <ctype.h> // READ BEFORE YOU START: // This homework is built on homework 06. The given program is an updated version of hw06 solution. It begins by displaying a menu to the user // with the add() function from the last homework, as well as some new options: add an actor/actress to a movie, display a list of movies for // an actor/actress, delete all movies, display all movies,...

  • 6.15 Program 6: Using Arrays to Count Letters in Text 1. Introduction In this program, you...

    6.15 Program 6: Using Arrays to Count Letters in Text 1. Introduction In this program, you will practice working with arrays. Your program will read lines of text from the keyboard and use an array to track the number of times each letter occurs in the input text. You will use the contents of this array to generate a histogram (bar graph) indicating the relative frequencies of each letter entered. Test cases are available in this document. Remember, in addition...

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