The arrays
1 2
|
char namelc[];
int freq[];
|
don't have any size, thus are not valid. These will be used to store the characters of the name, the size won't be greater than the number of letters in the name, so you could make them each the same as the length of the input array
1 2
|
char namelc[size];
int freq[size];
|
If the arrays are a bit larger than required it doesn't matter.
This line is not correct:
|
cin.getline (name,size,'\0');
|
the getline needs to check for the newline delimiter, '\n'. But since that is the default, it can be omitted, giving simply
|
cin.getline (name, size);
|
This loop doesn't change the character to lower case:
1 2 3
|
for (i = 0; i != '\0'; i++) {
tolower(name[i]);
}
|
because you need to store the value returned by the function,
|
name[i] = tolower(name[i]);
|
Also the for-loop condition is incorrect.
|
for (i = 0; i != '\0'; i++)
|
should be
|
for (i = 0; name[i] != '\0'; i++)
|
Next, you need to loop through the name again. This time, you need to search the array
namelc
to see whether the character is already stored there.
How would you do that? It will require a loop inside a loop. The array has a size of
size
but how many of those are in use? You need a counter, such as
int count = 0;
then have a loop that checks each character from 0 to count. If the character is not found, store the char in
namelc[count]
, store the integer 1 in
freq[count]
, and add 1 to
count
. But if the character is found, simply add 1 to
freq[k]
where
k
is the subscript of the matched character.
When the whole of the name has been processed, loop through the arrays from 0 to count-1, printing out the character and its frequency value.
I didn't mention skipping spaces here - I'd add in the check for that after the main structure has been coded.