How to find the number of escape sequences in any string?

  algorithm, c++, c++17, data-structures, string

Question : Find the number of "n"(new line character) present in any string.

Input : String s = "Shroud isn a godlike player inn video gaming community."

Output : 2

Here is my code :

    #include<bits/stdc++.h>
using namespace std;
int main()
{
   ios_base::sync_with_stdio(false);
   cin.tie(NULL);
   int nseq=0;
   string s;
   cin>>s;
   for(int i=0;i<s.length()-1;i++)
   {
       if(s.substr(i,i+1)=="n")
       {
           nseq++;
       }
   }
   cout<<nseq<<endl;
}

Source: Windows Questions C++

LEAVE A COMMENT