×

# 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<string>

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;


}

122
accept rate: 0%

 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! answered 12 Dec '18, 20:05 58●3 accept rate: 16% Thanks for ur help:) (14 Dec '18, 17:42)
 0 Just remove "-1" from k<=s[i].size()-1-j and you are good to go. You weren't checking all the substrings. answered 12 Dec '18, 20:13 127●6 accept rate: 27% 1 Thnaku i got my mistake:) (14 Dec '18, 17:42)
 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×630
×23

question asked: 12 Dec '18, 16:35

question was seen: 115 times

last updated: 14 Dec '18, 17:42