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>
usingnamespace 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.
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(unsignedint 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')
#include <iostream>
#include <cstring>
usingnamespace 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(unsignedint i = 0; i < in.size(); ++i)
{
if(!isspace(in[i])) to if(in[i]=='a')
++count;
}
return count;
}
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.
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();
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.