Help with String Problem

Homework assignment and I'm at my wits' end with this. I'm new to C++ and need a little help. I'm looking at creating a string, counting number of times a specific character appears (i.e. letter lowercase 'a') in the string, and then dividing character/s into length of string that will result in decimal form. Any help you can lend would be appreciated.

Basically output will read string sentence, number of times specific letter/s appears in sentence, and how many times it appears in sentence as a decimal.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <cstring>
using namespace std;

int main()

{
    string corporation("Amazon is an online corporation that has a vast 
    array of products and services that it sells.");
    double sentence = corporation.length();
    double percentage = 2.0/53.0; //I'm lost here for division aspect.
    cout << corporation << endl;
    cout << "The sentence is " << sentence << " characters long." << endl;
    cout << "a appears " << count(corporation.begin(), corporation.end(), 'a')  
    << " times, " << "which is " << percentage << endl;
    
    return 0;
}


Thanks for any input you can provide. It is greatly appreciated.
You need to actually count things first. std::count works on std::string just fine:
http://www.cplusplus.com/reference/algorithm/count/
http://en.cppreference.com/w/cpp/algorithm/count
Last edited on
Thanks LB!!!
For your percentage you can have the count/length
FYI, the length() will give the length of the string including spaces, if you want to include spaces then just doing length() is fine. But if want just the characters without spaces, you can make a loop to cycle through the string with a counter that only increases if the character being read is not a space. Like this:
1
2
3
4
5
6
7
8
9
10
int characterCount(std::string in)
{
    int count = 0;
    for(unsigned int i = 0; i < in.size(); ++i)
    {
        if (!isspace(in[i]))
            ++count;
    }
    return count;
}


You could do the same to count the 'a' character as well by just changing if(!isspace(in[i])) to if(in[i]=='a')
Does this look any better based on the suggestions provided? Thanks again for your help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <cstring>
using namespace std;

int main()

{
    string corporation("Amazon is an online corporation that has a vast 
    array of products and services that it sells.");
    double sentence = corporation.length();
    double percentage = 2.0/53.0; //I'm lost here for division aspect.
    cout << corporation << endl;
    cout << "The sentence is " << sentence << " characters long." << endl;
    cout << "a appears " << count(corporation.begin(), corporation.end(), 'a')  
    << " times, " << "which is " << percentage << endl;
    
    return 0;
}

    int characterCount(std::string in)
{
    int count = 0;
    for(unsigned int i = 0; i < in.size(); ++i)
 {
    if(!isspace(in[i])) to if(in[i]=='a')
    ++count;
 }
    return count;
}
Ditch everything after line 19.
Line 11 can be written with what you already have. Did you look at the links I provided?
LB - I did but, I'm trying to have it count the number of times 'a' is present in the string and then I need to come up with a computation for line 11 to divide number of times 'a' appears into string length. That is where I have been stuck. All other aspects of code work, but I'm stuck on the division part.

Thanks again for your help.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string> // #include <cstring>  ****
#include <algorithm> // **** required for std::count()
// using namespace std; // **** avoid

int main()

{
    const std::string corporation( "Amazon is an online corporation that has a vast "
                                   "array of products and services that it sells." );
    std::cout << corporation << '\n'
              << "The sentence is " << corporation.size() << " characters long.\n" ;

    // **** alert: not case-sensitive, will not count the 'A'
    const auto num_a = std::count( corporation.begin(), corporation.end(), 'a' ) ;

    std::cout << "'a' appears " << num_a << " times, which is "
              << num_a * 100.0 / corporation.size() << " percent.\n" ;

    // return 0;  // **** implied
}

http://coliru.stacked-crooked.com/a/f2e921f70814b8b6
Last edited on
To count the number of times 'a' appears: auto appearances = std::count(std::begin(corporation), std::end(corporation), 'a');

I don't know what confuses you with the division - the only thing to be aware of is that you may want to convert one or both values to a floating point type first so as to avoid integer division. Line 11 could be double percentage = static_cast<double>(appearances)/corporation.length();

EDIT: Changed 'line 111' to 'line 11'
Last edited on
Thank you for the help! Yeah, I was stuck on the division as I was trying double in integer form and I also missed line 18 with 100.0 multiplier before doing division. I greatly appreciate you breaking it down for me, JL.
Topic archived. No new replies allowed.