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