| | |
Frequency of characters entered.
![]() |
there are only 255 possibilities, so just make an int array 255 and use the character as the index
when done the elemenets of count > 0 is the frequency you want.
C Syntax (Toggle Plain Text)
int count[255] = {0} char c = 'A'; ++count[c];
Hi All,
The program is supposed to uppercase the characters entered, transpose them (A to B, B to C ... Z to A). And also keep a count of all alphaitalics, how many A's, how many B's .... Z's. Also a count for all other non-alphaitalic characters entered. At the end output which alphaitalic occured the most.
I cannot figure out how the program to count the characters. The upper case transformation works, but does not work for z to Z.
Thanks.
JONN.
The program is supposed to uppercase the characters entered, transpose them (A to B, B to C ... Z to A). And also keep a count of all alphaitalics, how many A's, how many B's .... Z's. Also a count for all other non-alphaitalic characters entered. At the end output which alphaitalic occured the most.
I cannot figure out how the program to count the characters. The upper case transformation works, but does not work for z to Z.
Thanks.
JONN.
c Syntax (Toggle Plain Text)
#include <iostream> using namespace std; const char DECLARED_SIZE = 96; void uppercase(char letters[]); int main() { cout << "ENTER UPTO 96 CHARACTERS, FOLLOWED BY A '#':\n"; char letters[DECLARED_SIZE], next; int index = 0; int count = 0; for(index = 0; index < DECLARED_SIZE; ++index)letters[index] = 0; index = 0; cin >> next; while ((next != '#') && (index < DECLARED_SIZE)) { letters[index] = next; index++; cin >> next; } letters[index] = next; int num_used = index; cout << "YOU ENTERED: \n"; for (index = 0; index < num_used; index++) cout << letters[index] << " "; cout <<endl; uppercase(letters); return 0; } //CHANGE LOWERCASE TO UPPERCASE. void uppercase(char letters[]) { int size = 0; int k = 0; char * answer; for (k = 0; letters[k] != '#'; k++) { size++; } answer = new char[size + 1]; for (k = 0; k < size; k++) { if ((letters[k] < 122) && (letters[k] > 96)) { answer[k] = letters[k] - 31; if ((letters[k] < 90) && (letters[k] > 64)) { answer[k] = letters[k] + 1; if (letters[k] = 122) { answer[k] = letters[k] - 57; if (letters[k] = 90) { answer[k] = letters[k] - 25; } } } } else { answer[k] = letters[k]; } cout << answer[k] << " "; cout <<endl; } return; }
Last edited by ~s.o.s~; Nov 1st, 2006 at 3:36 pm.
Please use code tags when posting code. Its not all that difficult and makes your post look a whole lot better.
>>for(index = 0; index < DECLARED_SIZE; ++index)letters[index] = 0;
This line is not necessary if you declare the array with an initializer, like this:
Isn't function uppercase() supposed to convert the array to all upper case characters? Then why did you make it so difficult? It can be done in only a couple lines of code
The code you posted does not do that.
You really do not need array letters at all, unless required by your instructor. what you need is an array of integers that keeps a count of the number of times a letter is entered. After you enter a character from the keyboard (or a file), convert the character to upper case then increment the array element (see my previous post for example).
>>for(index = 0; index < DECLARED_SIZE; ++index)letters[index] = 0;
This line is not necessary if you declare the array with an initializer, like this:
char letters[DECLARED_SIZE] = {0}, next;Isn't function uppercase() supposed to convert the array to all upper case characters? Then why did you make it so difficult? It can be done in only a couple lines of code
C Syntax (Toggle Plain Text)
void uppercase(char letters[]) { int k; for (k = 0; letters[k] != '#'; k++) letters[k] = toupper(letters[k]); }
•
•
•
•
I need a code that keeps a count of each letter entered, and any a count for all other non-letter symbols entered.
You really do not need array letters at all, unless required by your instructor. what you need is an array of integers that keeps a count of the number of times a letter is entered. After you enter a character from the keyboard (or a file), convert the character to upper case then increment the array element (see my previous post for example).
Last edited by Ancient Dragon; Nov 1st, 2006 at 8:04 am.
•
•
Join Date: Oct 2006
Posts: 57
Reputation:
Solved Threads: 2
Here's the code for a working solution i just made. What you do with the counters at the end is up to you though. Good luck!
C Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int main() { // counter is an int array containing a counter for each possible character // entered. counter[0] is used for 'a', counter[1] for 'b', counter[27] for // 'A' and so on. counter[52] is used for all other symbols int counter[53] = {0}; // MAX_SIZE is the maximum number of characters to read from the screen const int MAX_SIZE = 80; // letters is the array containing the inputted string char letters[MAX_SIZE]; cout << "Enter your string: "; cin.get(letters,MAX_SIZE); // here we will check all characters from the inputted string and allocate the // counters in the counter array accordingly for (int i=0; i<MAX_SIZE; i++) { switch (letters[i]) { case 'a': counter[0]++; break; case 'b': counter[1]++; break; case 'c': counter[2]++; break; case 'd': counter[3]++; break; case 'e': counter[4]++; break; case 'f': counter[5]++; break; case 'g': counter[6]++; break; case 'h': counter[7]++; break; case 'i': counter[8]++; break; case 'j': counter[9]++; break; case 'k': counter[10]++; break; case 'l': counter[11]++; break; case 'm': counter[12]++; break; case 'n': counter[13]++; break; case 'o': counter[14]++; break; case 'p': counter[15]++; break; case 'q': counter[16]++; break; case 'r': counter[17]++; break; case 's': counter[18]++; break; case 't': counter[19]++; break; case 'u': counter[20]++; break; case 'v': counter[21]++; break; case 'w': counter[22]++; break; case 'x': counter[23]++; break; case 'y': counter[24]++; break; case 'z': counter[25]++; break; case 'A': counter[26]++; break; case 'B': counter[27]++; break; case 'C': counter[28]++; break; case 'D': counter[29]++; break; case 'E': counter[30]++; break; case 'F': counter[31]++; break; case 'G': counter[32]++; break; case 'H': counter[33]++; break; case 'I': counter[34]++; break; case 'J': counter[35]++; break; case 'K': counter[36]++; break; case 'L': counter[37]++; break; case 'M': counter[38]++; break; case 'N': counter[39]++; break; case 'O': counter[40]++; break; case 'P': counter[41]++; break; case 'Q': counter[42]++; break; case 'R': counter[43]++; break; case 'S': counter[44]++; break; case 'T': counter[45]++; break; case 'U': counter[46]++; break; case 'V': counter[47]++; break; case 'W': counter[48]++; break; case 'X': counter[49]++; break; case 'Y': counter[50]++; break; case 'Z': counter[51]++; break; default : counter[52]++; break; } } return 0; }
may4life -- it may be a somewhat working solution (there is at least one bug I can spot), but much too long and too difficult. It can be greatly simplified by using an array of 255 then using the letter entered as the index into the array. That eleminates that huge switch statement.
Last edited by Ancient Dragon; Nov 1st, 2006 at 8:09 am.
The array of 96 characters is a limit on the number of characters you should enter from the keyboard. You need a another integer array to count the number of times a character was entered -- for example how may times did you enter the letter 'A', and how many times did you enter '1' ? Your program does not currently tell you that.
![]() |
Similar Threads
Other Threads in the C Forum
- Previous Thread: Need Help With Printing Pascal's Triangle In C
- Next Thread: Recursive function - checking for palindromes
| Thread Tools | Search this Thread |
adobe api array arrays binarysearch calculate char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators intmain() iso kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer posix power probleminc program programming pyramidusingturboccodes read recursion recv recvblocked repetition research scanf scheduling segmentationfault send shape socketprograming socketprogramming stack standard strchr string suggestions systemcall test unix urboc user variable voidmain() wab win32api windows.h






