C++ need help with arrays ( i think it must be done with arrays )

Posted on 2007-10-20
Last Modified: 2010-04-01
I am learning c++ . And i got task.
"In which given sentence word are most letters "a" ?"
It means that, when you write "Yesterday i ate Bananas", and it should give answer.
"The most letters "a" is in word Bananas".
i`m still learning that, but i don`t know how to do that. I googled alot, but still no answers, so i am trying my luck here.
And here is what i done

"#include <iostream.h>
#include <cstdlib>
#include <string>

using namespace std;

int i,n , m, length;
char s[80];

int main ()
    string s;  
    cout << "Enter Sentence:\n";
    std::getline(std::cin, s);
    i = s.length();
    cout << "You entered:" << s <<;


Question by:moonskyland
    LVL 7

    Accepted Solution

    Hi moonskyland,

    This task can be completed with a single pass through the string.
    Of course, you need to identify words in the sentence. Words consist of non-blank characters and are separated by blank characters.

    While you are iterating through array, for each word, you need to count number of occurrences of 'a' or 'A' character. If this number is larger than the current maximum you need to update it and save beginning and ending index for that word.

    When iteration ends you need to print substring of original string.

    Also pay attention to some special cases like:
    * Empty sentence.
    * Sentence consisting only of blank characters
    * Sentence consisting only of words without 'a' character.

    Task would be little more complicated if interpunction signs like .,; etc.. were allowed, then they would also be legal separators.

    Some useful links for string class:

    Author Comment

    And how to identify words in sentence ? I still don`t understand
    LVL 53

    Expert Comment

    >> And how to identify words in sentence ? I still don`t understand

    You don't have to accept a solution until your question has been answered ... just fyi ...

    To answer your question : words are separated by whitespace and/or characters like .,;!? etc. When you see one such character, then you know that the previous word has ended, and a new word is about to start.

    btw :

    1) Do not use <iostream.h>. It is deprecated. Use <iostream> instead.

    2) Since you added using namespace std; you don't have to use the std:: namespace qualifiers any more. It's not illegal, but unnecessary.

    3) Your main has to return an int. Make sure you do that ... Add return 0; before the end of main.

    4) You defined a lot of your variables globally ... It's better to define them inside the main.

    5) It's a good idea to initialize variables the moment you define them. It will avoid certain problems. So, instead of :

            int value;

        use :

            int value = 0;

        for example.

    Author Comment

    thanks for help!

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
    What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
    The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now