Search
 
SCRIPT & CODE EXAMPLE
 

CPP

letter occurrence in string c++

#include<bits/stdc++.h>
using namespace std;

 
int main(){
  int cnt=0;
   string s;
   cin>>s;
   int alpha[26]={0},i=0,j;
   while(s[i]!=''){
     if(s[i]>='a'&& s[i]<='z'){
       j=s[i]-'a';
       alpha[j]++;
     }
     i++;
   }
  for(int i=0;i<26;i++){
    if(alpha[i]>=1){
      cout<<(char)(i+'a')<<" : "<<alpha[i]<<endl;
    }
  }
  
  return 0;
}
Comment

find no of occurences of each letter in string c++

void countCharImproved(char *str) {
    std::map<char, int> count;
    int l = strlen(str);
    for(int i=0; i<l; i++) {
        count[str[i]]++;
    }
    for(const auto kvp : count) {
        std::cout << kvp.first << " occurs " << kvp.second << " times
";
    }
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: c shortest path dijkstra 
Cpp :: binary to int c++ bitset 
Cpp :: sort array in descending order c++ 
Cpp :: variable modulus 5 meaning in c++ 
Cpp :: void setup() { // put your setup code here, to run once:in m}void loop() { // put your main code here, to run repeatedly:} 
Cpp :: c program runner 
Cpp :: nand in cpp 
Cpp :: setFontSize QT 
Cpp :: c++ restrict template types 
Cpp :: PascalName seperate strings 
Cpp :: long, long long 32 bit or 8 bit in c++ 
Cpp :: https://www.cplusplus.com/doc/tutorial/pointers/ 
Cpp :: softwareegg.courses4u 
Cpp :: how to use mersenne_twister_engine in c++ to generate random numbers 
Cpp :: what do I return in int main() function c++ 
Cpp :: c++ map values range 
Cpp :: what is blob in computer vision 
Cpp :: assegnare valori in c++ 
Cpp :: C++ meaning :: 
Cpp :: traverse string in cpp 
Cpp :: c++ start process and get output 
Cpp :: determining a string is subsequence of another 
Cpp :: Explicit conversion casting 
Cpp :: Boats to Save People leetcode solution in c++ 
Cpp :: how to add 2 objects using operator overloading in c++ 
Cpp :: generate consecutive numbers at compile time 
Cpp :: how to delete repeated element in stack c++ 
Cpp :: codeform 
Cpp :: assert warning c++ 
Cpp :: Swap given nodes in a Doubly Linked List without modifying data 
ADD CONTENT
Topic
Content
Source link
Name
4+9 =