Advertisement
Filip_Markoski

LetterCounter

Jul 29th, 2017
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.30 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #include <cctype> /* isalpha() */
  4. #include<cstdio>
  5.  
  6. using namespace std;
  7.  
  8. char alphabet[256] = "abcdefghijklmnopqrstuvwxyz";
  9.  
  10. int countLetter(const char letter, FILE *f) {
  11.     cout << "In countLetter..." << endl;
  12.     /* Assumes it's a alpha letter and that there is a database */
  13.     int pos = 1;
  14.     cout << "Letter: " << letter << endl;
  15.     for (int i = 0; i < strlen(alphabet); ++i) {
  16.         if (alphabet[i] == letter) {
  17.             pos += i;
  18.         }
  19.     }
  20.     /* Store in Database */
  21.     fprintf(f, "%c %d\n", letter, pos);
  22.     return pos;
  23. }
  24.  
  25. int findInDatabase(const char letter, FILE *f) {
  26.     cout << "findInDatabase reached..." << endl;
  27.     /* Assumes that there is a database */
  28.     char line[256];
  29.     char currentLetter;
  30.     int currentValue = 0;
  31.     int letterValue = 0;
  32.     int numberOfLines = 0;
  33.  
  34.     while (!feof(f)) {
  35.         //fseek(f, numberOfLines, SEEK_SET);
  36.  
  37.         /*
  38.         printf("fgets(): \n");
  39.         fgets(line, 256, f);
  40.         cout << "The line:" << line << endl;
  41.          */
  42.  
  43.         printf("fscanf(): ");
  44.         fscanf(f, "%c %d\n", &currentLetter, &currentValue);
  45.  
  46.         cout << "L:" << currentLetter << " V:" << currentValue << endl;
  47.         if (currentLetter == letter) {
  48.             letterValue = currentValue;
  49.             break;
  50.         }
  51.         numberOfLines++;
  52.     }
  53.     return letterValue;
  54. }
  55.  
  56. int countWord(const char *w) {
  57.     cout << "In countWord..." << endl;
  58.     int total = 0;
  59.     FILE *f = fopen("worddb.txt", "a+");
  60.     int databaseResult = 0;
  61.     cout << "After opening file..." << endl;
  62.     for (int i = 0; i < strlen(w); ++i) {
  63.         rewind(f);
  64.         if (isalnum(w[i]) && f != NULL) {
  65.             databaseResult = findInDatabase(w[i], f);
  66.             if (databaseResult) {
  67.                 total += databaseResult;
  68.                 cout << "Total: " << total << endl;
  69.             } else {
  70.                 total += countLetter(w[i], f);
  71.             }
  72.         }
  73.     }
  74.     fclose(f);
  75.     return total;
  76. }
  77.  
  78. int main() {
  79.     char word[256];
  80.     while (1) {
  81.         cout << "Enter a word: ";
  82.         cin >> word;
  83.         if (strcmp(word, "quit") == 0) {
  84.             break;
  85.         }
  86.         cout << word << " has a total of " << countWord(word) << endl;
  87.     }
  88.     return 0;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement