Help with Converting between letters and numbers using arrays...

This is a discussion on Help with Converting between letters and numbers using arrays... within the C++ Programming forums, part of the General Programming Boards category; I think i've got it but not too sure, and If anyone knows any other way to do this how ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    34

    Help with Converting between letters and numbers using arrays...

    I think i've got it but not too sure, and If anyone knows any other way to do this how could i do it?
    Code:
    #include<iostream>
    #include<cctype>
    #include<string>
    #include<fstream> 
    #include<iomanip>
    using namespace std;
      
    char integer_to_letter(int);
    int letter_to_integer(char);
    void print_header();
    
    int main(int argc, char *argv[])
    {
       const int SIZE = 26;
       int freq[SIZE] = {0};
       char letter;
       int index;
       ifstream fin(argv[1]);
       ofstream fout(argv[2]);
       print_header();
       fin >> letter;
       while(!fin.eof())
         {
         if(isalpha(letter))
           {
           index = letter_to_integer(tolower(letter));
           freq[index]++;
           }
         fin >> letter;
         }
       fout << "Count the occurances of letters in a file" << endl
    	<< "producing a frequency" << endl << endl
    	<< "letter freq           letter freq" << endl
    	<< "------ -------------- ------ ---------------" << endl;
       for(int i = 0; i < 13; i++)
         {
         char one = integer_to_letter(i);
         tolower(one);
         char two = integer_to_letter(i+13);
         tolower(two);
         fout << right << setw(6) << one << left << " "
    	  << setw(15) << freq[i] << " "
    	  << right << setw(6) << two << " "
    	  << left << freq[(i+13)] << endl;
         }
       fin.close();
       fout.close();
       return 0;
    }
    
    /*
     * Name: letter_to_integer
     * Purpose: Convert a letter to the corresponding number
     * Receive: Data from a file
     * Return: Number corresponding to the letter
     */
    int letter_to_integer(char letter)
    {
      int index = (letter - 'a');
      return index;
    }
    
    /*
     * Name: integer_to_letter
     * Purpose: Convert a number to the corresponding letter
     * Receive: Data from a file
     * Return: Letter corresponding to the number
     */
    char integer_to_letter(int index)
    {
      return(static_cast<char>('a' + index));
    }
    
    /*
     * Name: print_header
     * Purpose: Display header
     * Receive: Nothing
     * Return: Header
     */
    void print_header()
    {
      cout << "\n";
      cout << "\n";
      cout << "\n";
    }
    Last edited by MyglyMP2; 04-18-2005 at 08:07 PM.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Code:
    for(int i = 0; i < 13; i++)
    {
    	 char one = integer_to_letter(i);
    	 tolower(one);
    	 char two = integer_to_letter(i+13);
    	 tolower(two);
    What do those lines do?

  3. #3
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    I assume you want to do this so that you may order your words in alphabetical order. However, you can do this using the STL which is easier.

    If this is not what you want to do then ignore this post.

  4. #4
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168
    Quote Originally Posted by 7stud
    Code:
    for(int i = 0; i < 13; i++)
    {
    	 char one = integer_to_letter(i);
    	 tolower(one);
    	 char two = integer_to_letter(i+13);
    	 tolower(two);
    What do those lines do?
    Click me!

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Explanation: Tolower will return the ASCII value for the lowercase equivalent of the ASCII character chr, if it is a letter.
    ...and the question still stands: what do those lines do? Try this Welshy:
    Code:
    char ch = 'A';
    tolower(ch);
    cout<<ch<<endl;
    and you'll see what I mean.
    Last edited by 7stud; 04-19-2005 at 08:08 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to use case/switch with letters and numbers
    By jericjones45 in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 04:38 PM
  2. Numbers to Letters
    By flip114 in forum C Programming
    Replies: 5
    Last Post: 09-24-2003, 03:18 AM
  3. converting numbers to letters??
    By Lau in forum C Programming
    Replies: 5
    Last Post: 11-06-2002, 11:17 AM
  4. Convert Phone Letters To Numbers
    By nicolobj in forum C Programming
    Replies: 4
    Last Post: 10-03-2002, 03:53 PM
  5. Transforming letters to numbers...
    By N8760 in forum C++ Programming
    Replies: 2
    Last Post: 12-23-2001, 02:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21