string array

This is a discussion on string array within the C++ Programming forums, part of the General Programming Boards category; How will i make a program that will read a line of text and output the number of occurance each ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    10

    string array

    How will i make a program that will read a line of text and output the number of occurance each letter. Assume that the input characters must consist entirely of letters, whitespaces, commas, and period.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>How will i make a program...
    ... do some research!

    Find out how to getline from a user.
    Find out how to loop.
    Find out how to compare two letters.

    Write something, and post it here when you're stuck.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >How will i make a program that will read a line of text and output the number of occurance each letter.
    Code:
    #include <iostream>
    #include <map>
    
    int main()
    {
      std::map<const char, int> frequency;
      std::map<const char, int>::iterator it;
    
      char input;
    
      while ( std::cin.get ( input ) ) {
        it = frequency.find(input);
    
        if ( it == frequency.end() )
          frequency[input] = 1;
        else
          it->second++;
      }
    
      for ( it = frequency.begin(); it != frequency.end(); ++it )
        std::cout<< it->first <<": "<< it->second <<std::endl;
    
      return 0;
    }
    Of course, if this is a homework problem that you were hoping we would write for you, the above code is useless. But it may give you some ideas.

    Toodles!

    -Prelude
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    10

    Smile string array

    can u give me a simplier code or program. my professor kinda new. he don't like complicated stuff which will scratch his head for week reading that code. thanks for the reply goddess Prelude
    and i'm kinda stuck

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: string array

    >>can u give me a simplier code or program.
    No. Start working it out yourself, then post some code when you're stuck.

    Read this in full before posting again please.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >my professor kinda new.
    That was the point entirely. We don't write your homework for you, that defeats the purpose of it.

    >he don't like complicated stuff which will scratch his head for week reading that code.
    This sounds like you need a new professor. If your instructor can't figure out a simple frequency check with (very) simple STL usage, she/he shouldn't be teaching C++.

    Here is a hint for a simple implementation that may work for you. Build two arrays, one that holds all of the letters being input and one that holds the frequency of each letter. The two array indices should correspond:

    char letters[256];
    int frequency[256];

    if letters[5] == 'a' then
    frequency[5] == number of a's

    When you read a letter, search the letters array from start to finish. If the letter already exists then add one to the number in the corresponding frequency index. If the letter does not exist then add it to the end of the array.

    This is a messy and inelegant solution and I probably completely confused you, but it is still a solution.

    -Prelude
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    10
    thanks prelude. and i think i need a new professor.

    i'll work it out from your hint it's a great help. till again

  8. #8
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    I would suspect very few c++ profesors would be able to
    say for certain if the code is absoutly correct since most professors arn't up to date with the latest
    libraries Try to understand this sort
    http://www.cs.cf.ac.uk/user/C.L.Mumf...CountPage.html

  9. #9
    Registered User
    Join Date
    Sep 2002
    Posts
    10
    Dear Prelude here's wat i have came up
    Attached Files Attached Files

  10. #10
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    Try this on for size: My C++ teacher doesn't even know what a class is!

    (but this is just a highschool and he's the only one who is willing to teach it. And he learned it specifically for the class, I believe)

  11. #11
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    You are right not all teachers know C++ and all the benefits of using the STL.

    Beside they have a lot of non-certified teachers teaching programming in high school (C++ & Java). Those that do (most) teach towards the AP CS exam.

    Mr. C.

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Dear Prelude here's wat i have came up
    Have you tried to compile this? If not (most likely) then you should, because there are plenty of syntax errors that will keep the program from working properly.

    As for the logic, it needs work. Here is pseudocode:
    Code:
    begin
      create input = 0 // Single character
      create letters[256]
      create frequency[256];
    
      repeat i = 0
        frequency[i] = 0
        i = i + 1;
      until i >= 256
    
      print "Enter Your Text Followed By EOF: "
    
      create letter_count = 0;
    
      while letter_count < 256
        read input
    
        if input == EOF
          break;
    
        create exists = false;
    
        if input is letter // Process letters only
          repeat i = 0
            // If input has already been read, increment the freqency
            if letters[i] == input
              frequency[i] = frequency[i] + 1;
              exists = true;
    
              break;
            endif
          until i >= letter_count
    
          // If input was not already processed, add 
          // it and set the frequency to one
          if exists != true
            letters[letter_count] = input;
            frequency[letter_count] = 1;
    
            letter_count = letter_count + 1;
          endif
        endif
      loop
    
      repeat walk = 0
        print letters[walk] + ": " + frequency[walk] + "\n";
      until walk >= letter_count
    end
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer to array of string and Array of Pointer to String
    By vb.bajpai in forum C Programming
    Replies: 2
    Last Post: 06-15-2007, 07:04 AM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 10:11 AM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 04:58 PM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 01:56 PM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM

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