ANY HELP WOULD B APPRECIATED?

This is a program to calculate the number of vowels in all the substrings of a given string for a number of test cases.

MY CODE:
#include //NOTE that for string “baceb” it gives answer 10 but shud give 16.

#include

using namespace std;

static int count=0;

inline void count1(string s)

{

for(int i=0;i<s.size();i++)

{

    if(s.at(i)=='a' ||s.at(i)=='e' ||s.at(i)=='i' ||s.at(i)=='o' ||s.at(i)=='u' || s.at(i)=='A' 

||s.at(i)==‘E’ ||s.at(i)==‘I’ ||s.at(i)==‘O’ ||s.at(i)==‘U’)

    {count++;}
    
}

}

int main()

{

int t;

cin>>t;

string s[t];

for(int i=0;i<t;i++)

{

    cin>>s[i];

    
}

for(int i=0;i<t;i++)

{

    for(int j=0;j<s[i].size();j++)

    {

        for(int k=1;k<=s[i].size()-1-j;k++)

       {count1(s[i].substr(j,k));}   

    }

    cout<<endl<<count;

    count=0;

}


return 0;

}

Hi, @chaitu17789

Your problem is in this segment of code, in particular in the inner loop.

       for(int j=0;j < s[i].size();j++)
        {
            for(int k=1; k <= s[i].size()-1-j; k++)
            {
                count1(s[i].substr(j,k));
            }
        }

For example, if j (string starting position) is currently 0 and size() returns 5, I believe you want k (substring length) to range from 1 to 5. This is because when starting at position 0, you can have substrings of all lengths up to size().

To further the example, let s[i] be baceb. Starting from 0, possible substrings: b, ba, bac, bace, baceb

But your inner loop does not consider all possible substrings starting from that position.

I hope this helps!

Just remove “-1” from k<=s[i].size()-1-j and you are good to go.

You weren’t checking all the substrings.

Thnaku i got my mistake:)

1 Like

Thanks for ur help:)