Generation

generate functionSun, 30 Apr 2023

write a code to take a string as input and print no. of distinct alphabets in it.

#include <iostream> #include <string> using namespace std; int main() { string str; cout << "Enter a string: "; getline(cin, str); int dist[26] = {0}; for (int i = 0; i < str.length(); ++i) { if (str[i] >= 'a' && str[i] <= 'z') { dist[str[i] - 'a']++; } } int count = 0; for (int i = 0; i < 26; ++i) { if (dist[i] > 0) { count++; } } cout << "No. of distinct alphabets: " << count << endl; return 0; }

Questions about programming?Chat with your personal AI assistant