Hi,

First time post here. I understand that you all like to have attempted code, but I just have a conceptual question. My task is to create a C program that takes in a user inputted string and counts the character frequency. I am given a skeleton file with the Count function and two arrays one for the string the other is a length of 26 and the number of each letter occurance is to be stored in that array. I also have print message function provided that in one of its cases will divide each letter by the total amount.

For the input i just want to use scanf(), and then it says that the Count function MUST use a getchar() to read one letter of the string at a time and go until ENTER or \n is pressed. If i use scanf like this: scanf("%s", stringarray) which stores each character into that string ,I assume, how do I use getchar() in all of this to get the number that each letter occurs into the other array of 26. To get those into their appropriate spots I am planning on simply subtracting 'a' from the character and then going to that location and incrementing it. IE if my first letter is b I subract a from b and then the location 1 is incremented.

Basically, I just need to take in the string put it in an array using getchar() then use it to get the counts of each letters into the other array. and the skeleton code is provided with a function that will divide for me.

Any advice and input would be appreciated.

Thanks!

This would make more sense if you posted the skeleton code along with the explanation...

Sorry, I did re read it and it does sound confusing. However I do not have access to the skeleton file at the moment, but I will try to clarify... this is straight from the assignment "the file needs to be modified and you need to implement the Count function, which reads in a character one at a time, using getchar, until the ‘Enter’ key or ‘\n’ has been entered"

1 array of 200,000 length for the string input
1 array of 26 for the letter count of each letter, that means either upper or lower case letter would be stored in the same location. A or a would both increment the 0 location of the 2nd array

then...

"After the user input, the count
function displays the frequency of each of the letters that were entered"

Why would you need an array of 200000 bytes?...

The simplest way to do this is, get the getchar() working first. Just creat a while loop and read in the input characters until '\n' is recieved.
The characters, as they are read in the while loop, can be evaluated for the postion in the frequency array...

Obviously as you said:

loop getchar() till (key == '\n')
    if is_alpha()
      if is_upper_case()
        increase array_counter[key - uppercase_A_as_an_int];
      else
        ...

Write it out as pseudo-code then put it into C format.

The array is 200,000 because the string can have up to 200,000 characters inputed in it.

At the moment I am also not concerned about upper case or lower case they will only be testing lower case letters.

The array is 200,000 because the string can have up to 200,000 characters inputed in it.

Why? Read one character at a time. You need 1 character, not 200K.

I understand, but that is what's in the skeleton code so it has to stay.

lugan> For the input i just want to use scanf(), and then it says that the Count function MUST use a getchar() to read one letter of the string at a time and go until ENTER or \n is pressed.

There's a conflict as per the description. getchar() reads only from stdin, which is the standard input (keyboard) one character at a time.
scanf() does read from stdin as well. However, once it is in a string buffer, getchar() will not work for what you want.


lugan>If i use scanf like this: scanf("%s", stringarray) which stores each character into that string
scanf() will stop reading at the first blank space that it finds or the newline. That means that you can not have spaces in the input string.

I was just confused about the abilities of getchar(), I got it it was very simple. Scanf is not even necessary. Thanks for your input guys.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.