Answer to Question #44935 in C++ for sasikala

Question #44935
to find out whether the string is super ascii or not
1
Expert's answer
2014-08-21T03:38:47-0400
//Answer on Question#44935 - Progamming - C++
#include <iostream>
#include <string>
using namespace std;
bool is_super(string s) {
int cnt['z' - 'a' + 1] = {}; // an array with the number of occurrences of each letter
for (int i = 0; i < s.length(); ++i) { // calculate the number od occurreces of each letter
++cnt[s[i] - 'a']; // s[i] - 'a' is the code of the letter s[i]
}
bool res = true;
for (int i = 0; i <= 'z' - 'a'; ++i) {
if (cnt[i] != i + 1 && cnt[i] > 0) res = false; // the array is indexed from 0 but the codes are starting from 1, so we check for cnt[i] != i + 1
}
return res;
}
int main() {
string s;
cin >> s;
if (is_super(s) == true) cout << "Is super ASCII" << endl;
else cout << "Isn't super ASCII" << endl;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS