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

Posted on 2007-10-20
Medium Priority
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
  • 2

Accepted Solution

UrosVidojevic earned 1500 total points
ID: 20115184
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

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

Expert Comment

ID: 20115406
>> 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

ID: 20115450
thanks for help!

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

862 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