Thread: Word unscrambling

  1. #31
    Registered User
    Join Date
    Jan 2006
    Posts
    19
    Ok. So, is the ctrie part the letter count thing?

    Also, do you have msn? It would be easy to talk that way

    And thanks again for the help

  2. #32
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    ok let's go back to basics:


    Do you know how to loop through an array of characters?

    So let's say I have an array

    char array[100] = "treenef";

    Code:
    +--+--+--+--+--+--+--+
    | t|r |e |e |n |e | f|
    +--+--+--+--+--+--+--+
     
     0   1  2  3  4  5  6



    to loop through each character I would do this:

    Code:
    for (int i=0; i<7; i++)
    {
       cout<<array[i]<<endl;
    }

  3. #33
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Or better yet
    Code:
    #include <cstring>
    
    size_t len = strlen(array);
    for(size_t i = 0; i < len; i ++) {
        cout << array[i] << endl;
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #34
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    And then, instead of couting the characters, you could count them . . .
    Code:
    #include <climits>
    #include <cstring>
    #include <cctype>
    
    unsigned charcount[UCHAR_MAX] = {0};
    size_t len = strlen(array);
    
    for(size_t i = 0; i < len; i ++) {
        charcount[array[i]] ++;
    }
    
    for(int x = 0; x < UCHAR_MAX; x ++) {
        if(charcount[x]) {
            cout << '\'' << isprint(x) ? x : '-' << "': " << charcount[x] << endl;
        }
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #35
    Registered User
    Join Date
    Jan 2006
    Posts
    19
    Yes I understand that, It would print all 7 charactors stored in the array.
    Last edited by pukebucket; 01-07-2006 at 02:47 PM.

  6. #36
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    OK, that's good.

    So the word treenef...

    has three 'e's
    one 'f'
    one 't'

    etc.

    Would you know how to write a program to do that?

  7. #37
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    My first post would [edit]"print all 7 charectors stored in the array"[/edit], and treenef's one would too, but not my most recent one. My previous post would print something like
    Code:
    e: 3
    f: 1
    n: 1
    r: 1
    t: 1
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #38
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Would you know how to write a program to do that?
    Too late.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #39
    Registered User
    Join Date
    Jan 2006
    Posts
    19
    I see. No I don't think I could. My tutor is looking pretty bad right now. He taught us nothing like that.

    To dwk: I'm not sure that I understand your code.

    Sorry to be a pain people

  10. #40
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    I see...

    Erm in regards to dwks code, it's probably a bit too complicated.

    You need something more intuitive...

    Have a look at this and see if you understand the concept behind it:

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        char array[100]="treenef";
        
        int size = strlen(array);
        
        char alphabet[50]="abcdefghijklmnopqrstuvwxyz";
        
        int letter_count[26];
        for(int i=0; i<26; i++)
        {
            letter_count[i]=0;
        }
            
        
        for(int i=0; i<size; i++)
        {
            for(int j=0; j<=26; j++)
            {
                if(array[i]==alphabet[j])
                {
                    letter_count[j]++;
                }
            }
        }            
            
           for(int j=0; j<26; j++)
            {
                
              cout<<alphabet[j]<<" "<<letter_count[j]++<<endl;
                
            }
        
        cin.get();
        return 0;
    }

  11. #41
    Registered User
    Join Date
    Jan 2006
    Posts
    19
    Ok I'll try to explain your code to show how much I understand.

    Code:
    char array[100]="treenef"; //Declares a charactor array called array,
     of 100 bytes and saves treenaf into it
        
        int size = strlen(array); //Declares an integer called size,
     Then it assigns its value as the length of the array called array
        
        char alphabet[50]="abcdefghijklmnopqrstuvwxyz"; // Declares
     a charactor array called alphabet, of 50 bytes. Then it saves the aphabet into it.
        
        int letter_count[26]; // Declares an integer called letter_count, consisting of 26 bytes.
    
        for(int i=0; i<26; i++) // A loop. It creates an integer called i 
    and assigns it a value of 0. It then says that as long as i is under 
    26 to run the next code. The i++ makes the integer i increase by 1 each loop.
    
        {
            letter_count[i]=0; // Im not sure. It looks like it assigns each
    
     letter of the alphabet to 0?
        }
            
        
        for(int i=0; i<size; i++) //Another loop. Assigns integer i to 0, 
    loops if i is under the variable size, and i increases by 1 each loop.
    
    
        {
            for(int j=0; j<=26; j++) //Another loop. Sets j to 0, loops if j 
    is bigger or equal to 26, j increases by 1 each loop
    
            {
                if(array[i]==alphabet[j]) //Hrm. Another loop, if value i 
    array is eqaul  to alphabet value j then do the next code.
    
                {
                    letter_count[j]++; //Not sure
                }
            }
        }            
            
           for(int j=0; j<26; j++) //Same as the one above
            {
                
              cout<<alphabet[j]<<" "<<letter_count[j]++<<endl; // Print 
    the alphabet value j, a space, then the letter_count j. Not sure 
    what this means.
                
            }
        
        cin.get();
        return 0;
    }
    Hrm, I understand quite a bit (I think, and hope) but theres still a
    bit I don't quite get. Sorry about the way its layed out :S
    Last edited by pukebucket; 01-07-2006 at 03:17 PM.

  12. #42
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    Yeah some of what u got is right some wrong.

    Draw out a flow chart to and follow each bit with your finger.

    Think about it for a day or so and then come back. I'll explain any stuff you don't understand

    I've got to go now.

    However, you should be able to see the reason why a letter frequency count is useful for your problem.


    Code:
    treenef,  feneert,  eertnef,
    All have the same letter frequency count.

  13. #43
    Registered User
    Join Date
    Jan 2006
    Posts
    19
    Ok thanks. I understand why I need it now. Thanks for all the help. If I need more I'll come and ask, as for now, I got some learning to do!

    Thanks and bye people.
    Thanks alot treenef, your help has been invaluable, so was everyone elses.
    Last edited by pukebucket; 01-07-2006 at 04:26 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  2. please help with binary tree, urgent.
    By slickestting in forum C Programming
    Replies: 2
    Last Post: 07-22-2007, 07:55 PM
  3. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  4. Wrong Output
    By egomaster69 in forum C Programming
    Replies: 7
    Last Post: 01-28-2005, 06:44 PM
  5. Using 'if' with char arrays or string objects
    By c++_n00b in forum C++ Programming
    Replies: 36
    Last Post: 06-06-2002, 09:04 PM