-1

I was asked to do a program for an assignment which sounds something like this:

Create a program to which you pass a text file with any number of words separated by a colon. The program will create a new file where the letters of the alphabet will be written (from A to Z), each on a new line, followed by a number of words from the input file that begins with the respective letter of the alphabet.

At first, it appeared to be quite easy to me. I was able to read the file, find the first letter in all the words and get them to appear in the console.

Here is the point that I am stuck on. I have no clue how to proceed. I am aware that I should use an array, which I could later use to get the required numbers, but I am not for the love of god able to make it work.

Here is what I came up with so far:

#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
#include <stdio.h>


using namespace std;

int main() {
ifstream fin("test.txt");
char ch;
string word;
int alphabet [26];

while (fin.get(ch))
{
    if (isspace(ch))  
    {
        continue;
    }
    else if (ch == ':') // found the end of a word
    {
        char first_letter = toupper(word[0]);
        cout << first_letter << '\n';
        word.clear(); 
    }
    else  
    {
        word += ch;
    }
}

if (word.size() > 0)
{
    char first_letter = toupper(word[0]);
    cout << first_letter << '\n';
}
}

Just to clarify. The input should look something like this - video:Elizabeth:Martin:service:work:place:British:file:stream:movie:song:quake:love:hate:York etc

And the output should look like this -

A 0
B 1
C 0
D 0
E 1
F 1
G 0
H 1
I 0
J 0
K 0
L 1
M 2
N 0
O 0
P 1
Q 1
R 0
S 3
T 0
U 0
V 1
W 1
X 0
Y 1
Z 0
halfer
  • 19,824
  • 17
  • 99
  • 186
Uchida
  • 13
  • 2
  • 2
    First, you need to initialize `alphabet` array to all zeros. Next, you need to look at the char code of the letter and calculate the index into the array based on that. – 001 May 30 '18 at 17:08
  • So.. What exactly is your question? Does your code not produce correct output? If so, how does it differ from the expected one? Did you try stepping through your code with a debugger? – Algirdas Preidžius May 30 '18 at 17:09
  • Don't use C-style arrays in C++. Do use [`std::vector`](http://en.cppreference.com/w/cpp/container/vector). These can be initialized with a default value where C arrays won't be. – tadman May 30 '18 at 17:12
  • Related: [count all letters, not only the first letter](https://stackoverflow.com/a/17628697/509868) – anatolyg May 30 '18 at 17:20

2 Answers2

8

Break the problem up into smaller problems.

  1. Write a program that takes a text file and prints its contents
  2. Modify the program to splits the words by colon and prints each word on it's own line
  3. Modify the program to print only the first character of each word
  4. Modify the program to count the number of instances of each letter (keep printing the letter) - print out the counts when done
  5. Modify the program to not print out the letters

Now you're done.

At every point along the way you are making a single change to the logic which means you can isolate both your thought process and debug mistakes much more easily.

Robert Horvick
  • 3,966
  • 21
  • 18
1

You want to store the characters in the array by looking at the character byte code. First, set alphabet to an array of all 0's. Then count the first letter of each word with the index representing the letter.

char first_letter = toupper(word[0]);
alphabet[first_letter - 'A']++; 

Then, at the end, print out:

for(int i = 0; i < 26; ++i) {
    printf("%c %d\n", i + 'A', alphabet[i]);
}
samuelnj
  • 1,627
  • 1
  • 10
  • 19
  • 1
    `//65 is the ascii code for A` the fact that you added a comment should have been a hint that using a magic constant was a bad idea. As Barmar said, use `'A'` (P.S: [it's not guaranteed to be ASCII](https://stackoverflow.com/q/29381067/583833) so that code isnt even portable) – Borgleader May 30 '18 at 17:25