Display the number of ASCII- sequence

I am able the display all the ASCII- subsequences, but not able to print the number of ASCII- subsequences. how can I do that?

code-

#include
#include
using namespace std;

//function to print subsequesnces containg ASCII value of the characters
//or the characters of the given string
void findsub(string str, string res, int i){

//base case
if(i == str.length()){

    //if length of the subsequence exceeds 0
    if(res.length() > 0){

        //print the subsequence
        cout << res <<" ";
    }
    
    return;
}


//stores character present at the i-th index of str
char ch = str[i];

//if the i-th character is not included in the subsequence
findsub(str, res, i+1);

//icluding the i-th charachter in the subsequence
findsub(str, res + ch, i+1);

//include the ASCII value of the i-th character in the subsequence
findsub(str, res + to_string(int(ch)), i+1);

}

int main(){

string str;
getline(cin,str);
string res ="";

//stores length of str
int N = str.length();

findsub(str, res, 0);

cout<<endl;



return 0;

}

Hi @tanishqbatra0511,

There is a small mistake in your code, you also need to print a sequence if its length equals 0 means an empty string. So, just change your base condition like this

if(res.length() >= 0)

Now, to count all ASCII sequences just declare a global variable cnt and increment it every time you print a sequence.

int cnt = 0;
void findsub(){
    //base case
    if(i == str.length()){
       if(res.length() >= 0){
            cout << res <<" ";
            cnt++;
       }
        return;
    }
    //do something
}

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

yes, got it, thanx for the reply

Hello @tanishqbatra0511,
Welcome :grinning:

1 Like