Hi, i need help on these question..i already search for the answer on the google and still not one of that help..i need to write a c program to count the occurrence of the input data and display it in alphabetical order.. for example i insert DSADSSAA and it need to show A=3,D=2,S=3..
so,can one of the expert help me..even the hint on how to count it also help me a lot..
6 3705
Use an array for the 26 counts.
From your ASCII table you can see that an 'A' is 65.
So read 1 character of input.
Subtract 65
Now you have the element number in he array.
Increment the count in that position.
So 'A' is 65. You subtract 65. Your result is 0 so increment array[0].
So 'B' is 66. You subtract 65. Your result is 1 so increment array[1].
Off you go.
hi weaknessforcats..
thx for for the help..right now i'm try to do your suggestion..and before that,i manage do the program,but it seem the program will be to long if i want to check and count the alphabet from a-z..the program is like below..
#include <stdio.h>
#define MAXNUM 15
int countchar (char []);
int main()
{
char message [MAXNUM];
int numchar;
int i, count = 0;
printf("User key in: ");
gets(message);
countchar (message);
getch();
} // END OF MAIN
//FUNCTION IMPLEMENTATION
int countchar (char list[])
{
int input,a=0,b=0,c=0,d=0,e=0,f=0, count = 0;
for (input = 0; list[input] != '\0'; input++)
{
if(list[input]=='a')
++a;
if(list[input]=='b')
++b;
if(list[input]=='c')
++c;
if(list[input]=='d')
++d;
if(list[input]=='e')
++e;
if(list[input]=='f')
}
if(a!=0)
printf("A = %d\n", a);
if(b!=0)
printf("B = %d\n", b);
if(c!=0)
printf("C = %d\n", c);
if(d!=0)
printf("D = %d\n", d);
if(e!=0)
printf("E = %d\n", e);
if(f!=0)
printf("F = %d\n", f);
return (count);
}
so,is there any way to make it more simple??
The idea is to calculate the array element to increment.
Just follow my steps:
Read one character
Subtract 65.
Increment the array element at that position.
If you are using capital and lower case letters then there are 52 counts:
You need to set these counts to zero.
So if you read a char then you would: - ++counts[char_I_read - 65];
-
and that's it. You do not need to test for the particuar character.
The array is a map. The first 26 elements are for A to Z and the last 26 are for a to z.
When you display the counts you can use this mapping to determine what letter to display. In this case ADD 65 to the array position:
So if you want to display counts[0] you can set a char to 0, then add 65 and wen you display the char you will see A.
Therefore in your display loop all you need is: - printf("%c %d\n", (char)(i+65+), counts[i]);
It seem i still cant do your method..i have been struggling for over a week and still cant do like you suggest..can you give me other example please..i'm really desperate right now..
OK. Here's the main(): - int main()
-
{
-
char input[80]; //the data entered
-
int counts[26]; //[0] is for A. [1] is for B, etc
-
-
InitializeCounts(counts, 26);
-
-
cout << "Enter your letters\n" << endl;
-
cin >> input;
-
-
AnalyzeData(input, counts);
-
DisplayCounts(counts, 26);
-
}
This solution just has the cuonts array. There are three functions. One function initializes the counts array elemets to 0. The second takes the inout and processes it to make he counts and the third displays the counts.
Note that an A has a value of 65. Moving 65 to a char and displaying the char will produce an A on the screen. The display function uses this fact to generate the letter to display based on the counts element position.
Here are the three functions: - void AnalyzeData(char* data, int* counts)
-
{
-
for(int i = 0; data[i] != '\0'; ++i)
-
{
-
++counts[data[i] - 65];
-
}
-
-
-
}
-
void DisplayCounts(int* counts, int numelements)
-
{
-
char temp;
-
for (int i = 0; i<numelements; ++i)
-
{
-
temp = i +65; //create letter to display
-
cout << temp << " " << counts[i] << endl;
-
}
-
}
-
void InitializeCounts(int* counts, int numelements)
-
{
-
-
for (int i = 0; i<numelements; ++i)
-
{
-
counts[i] = 0;
-
}
-
}
donbock 2,426
Recognized Expert Top Contributor
Another approach is to first sort the array; and then scan it from front to back, counting the length of each sequence of identical values. With this approach you don't need to make any assumptions about the nature of your input.
However, sometimes you want to make assumptions. For example, do you intend to treat 'A' and 'a' as different tokens? If not, then something more is needed. It might be as simple as using toupper() to convert all characters to uppercase in the sorting step.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Oeln |
last post by:
I've got the following issue: in one form (1.php), I get the number of
inputs to offer, $input_cnt (it could be from 1-10, for example). In a
for loop, I then offer that number of input fields in...
|
by: A Seel |
last post by:
COUNTING NUMBER OF SELECTS MADE
table mytable {
id, data, hits
}
|
by: Sourabh |
last post by:
Hello,
I am trying to write a VB.NET monitoring application for a MS SQL
server.
For that I need to know the following:
1. How do I count the number of read/write accesses per database on
the...
|
by: Grey |
last post by:
I have created a asp.net form for user to input data. After input the data,
user need to click a button to export the input data to excel for data
analysis with excel pivot table function. is it...
|
by: Magnus |
last post by:
I'm currently developing an application with classified information as
input to a couple of algorithms.
Which strategy should I use to protect the input data from beeing read?
The files should...
| |
by: JJ |
last post by:
Is it possible to count the number of columns that match certain conditions
and return a single value?
For example in the database we have a record where:
Column 1 = Male
Column 2 = A
Column...
|
by: jane007 |
last post by:
Hello everybody:
I am having a problem.
On Unix platform, there is a script that need user to input data from console, then when I call Unix perl script from
Windows, these is an issue occurs,...
|
by: raghunath1981 |
last post by:
Hi All,
Could you please help me with writing a sql for counting number of times a varchar (1000) repeated in the table. I think I cannot use groupby/distinct etc.
Please give me an idea how I...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |