Question

C++ Programming: Write a function that takes in a C++-style string s (from #include ), as...

C++ Programming:

Write a function that takes in a C++-style string s (from #include ), as well as an integer n, and then returns true if any substring of length n in s repeats itself, and false otherwise. For example, if s is "toe-to-toe" and n is 3, then the function would return true since "toe" occurs twice within s. However, if n were 4, the function would return false. As a second example, if s is "singing" and n is 2, then the function would return true since, e.g., "in" appears more than once (as does "ng"). In this problem you may use any of the C++ string member functions.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream> //for string operstions #include <string> //includiing the namespace standard using namespace std; //funcint main() int 1,m; l-ssc(TOE-TO-TOE, TOE); m= ssc (SINGING, ING); cout<< TRUE \n; TI. else cout<<FALSE \n; if (mDefault Term sh-4.3$ gt+ -std-c++11 -o main .cpp sh-4.3$ main TRUE TRUEcode:

#include <iostream>
//for string operstions
#include <string>
//includiing the namespace standard
using namespace std;
//function for calculating the substring
int ssc(const string& givenst, const string& stsub)
{
if (stsub.length() == 0) return 0;
int cnt = 0;
int n =2;
for (size_t offset = givenst.find(stsub); offset != string::npos;
   offset = givenst.find(stsub, offset + stsub.length()))
{
cnt++;
  
}
if(cnt==n)
{
return 1;
}
else
{
return 0;
}
  
}

int main()
{
int l,m;
l=ssc("TOE-TO-TOE", "TOE");
m= ssc("SINGING", "ING");
if(l==1)
{
cout<< "TRUE \n";
}
else
{
cout<<"FALSE \n";
}
if(m==1)
{
cout<< "TRUE \n";
}
else
{
cout<<"FALSE \n";
}
return 0;
}

Add a comment
Know the answer?
Add Answer to:
C++ Programming: Write a function that takes in a C++-style string s (from #include ), as...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT