Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

C++ counting letters

$
0
0
I have a "simple" code to write: take a string and count the letters.
Clearly missing basic step, but have tried everything I can find in my book and DIC tutorials, for the last 3 hours.
Added a "while" to the double "for" loops for control without success.
With it this way, I get "a: 1 times" constantly. Any advice would be appreciated.
#include <string>
#include <iostream>
using namespace std;

int main()
{
string sentence;
cout<<"Enter sentence: ";
getline(cin, sentence, '\n');
int counts[100];
const char s[] = {'a','b','c'.......};

count(s, counts, sentence);
return 0;
}
void count(const char s[], int counts[], string sentence){
int count = 0;
for(int i = 0; i < 100; i++){
for(int j = 0; j < 100; j++){
if(sentence[i] == s[j]){
count++;
}
if (count == 0){
count = 0;
}
else{
cout<<s[i]<<":"<<count<<" times"<<endl;
}
}
return;
}
}

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>