473,569 Members | 3,063 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 3708
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
2129
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 the next form (2.php): for($cnt=1; $cnt<=$input_cnt; $cnt++) { echo "<input type=\"text\" name=\"input.$cnt\">"; } I get, for example,...
3
2166
by: A Seel | last post by:
COUNTING NUMBER OF SELECTS MADE table mytable { id, data, hits }
1
1887
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 server since the creation of the DB? 2. How do I get the last access time of the database ? Any inputs would be really appreciated.
1
3133
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 possible to do that?? if so, any sample for me as i am a new in designing pivot table with asp.net million thanks
2
1676
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 be files on the local computer in the application directory structure and it must be possible to update the input data with an external application....
1
2114
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 3 = B Column 4 = D Column 5 = M
4
3774
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, when I input data and enter "enter" so fast, the Windows console is freezed, I don't know why, does anybody know?Thank you very much. My code...
3
3444
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 could do that if I want the output such that each row is the varchar (distinct) and number times it repeated. I was trying several things but it...
0
7703
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7930
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8138
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7983
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3662
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2118
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
950
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.