473,508 Members | 2,040 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

to counting the number of the occurrences in the input data and sort it in alphabet

3 New Member
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..
Dec 22 '11 #1
6 3705
weaknessforcats
9,208 Recognized Expert Moderator Expert
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.
Dec 22 '11 #2
sodens48
3 New Member
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??
Dec 22 '11 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
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:

Expand|Select|Wrap|Line Numbers
  1. int counts[52];
  2.  
You need to set these counts to zero.

So if you read a char then you would:

Expand|Select|Wrap|Line Numbers
  1. ++counts[char_I_read - 65];
  2.  
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:

Expand|Select|Wrap|Line Numbers
  1. printf("%c %d\n", (char)(i+65+), counts[i]); 
Dec 22 '11 #4
sodens48
3 New Member
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..
Jan 5 '12 #5
weaknessforcats
9,208 Recognized Expert Moderator Expert
OK. Here's the main():
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3. char input[80];   //the data entered
  4. int counts[26];   //[0] is for A. [1] is for B, etc
  5.  
  6. InitializeCounts(counts, 26);
  7.  
  8. cout << "Enter your letters\n" << endl;
  9. cin >> input;
  10.  
  11. AnalyzeData(input, counts);
  12. DisplayCounts(counts, 26);
  13. }
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:

Expand|Select|Wrap|Line Numbers
  1. void AnalyzeData(char* data, int* counts)
  2. {
  3.       for(int i = 0; data[i] != '\0'; ++i)
  4.       {
  5.            ++counts[data[i] - 65];
  6.       }
  7.  
  8.  
  9. }
  10. void DisplayCounts(int* counts, int numelements)
  11. {
  12.     char temp;
  13.           for (int i = 0; i<numelements; ++i)
  14.           {
  15.                     temp = i +65;   //create letter to display
  16.                     cout  << temp << "  " << counts[i] << endl;
  17.           }
  18. }
  19. void InitializeCounts(int* counts, int numelements)
  20. {
  21.  
  22.           for (int i = 0; i<numelements; ++i)
  23.           {
  24.                     counts[i] = 0;
  25.           }
  26. }
Jan 5 '12 #6
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.
Jan 10 '12 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

1
2122
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...
3
2161
by: A Seel | last post by:
COUNTING NUMBER OF SELECTS MADE table mytable { id, data, hits }
1
1885
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...
1
3128
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...
2
1668
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...
1
2110
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...
4
3768
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,...
3
3440
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...
0
7226
marktang
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,...
0
7125
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...
0
7388
jinu1996
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...
1
7049
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...
0
7499
tracyyun
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...
0
5631
agi2029
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,...
0
3199
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...
1
767
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
422
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.