Answers for "count total elements in string c++"

C++
2

string count occurrences c++

#include <algorithm>

std::string s = "a_b_c";
size_t n = std::count(s.begin(), s.end(), '_'); //n=2
Posted by: Guest on April-18-2021
0

count word accurances in a string c++

string s;
  string sub;
  int i=0, j, temp, counter=0, chk;
    cout<<"Enter the String: ";
    getline (cin, s);
    cout<<"Enter the Word: ";
    getline (cin,sub);
    
    while(s[i]!='\0')
    {
        temp = i;
        j=0;
        while(sub[j]!='\0')
        {
            if(s[i]==sub[j])
                i++;
            j++;
        }
        chk = i-temp;
        if(chk==j)
            counter++;
        i = temp;
        i++;
    }
    cout<<"\nOccurrence of '"<<sub<<"' = "<<counter;
    cout<<endl;
    return 0;
Posted by: Guest on March-05-2022

Code answers related to "count total elements in string c++"

Browse Popular Code Answers by Language