Vonn Vonn - 1 month ago 8
C++ Question

difference between string size() function and strlen in this particular case

I recently did this question

Specification:


Input Format The first line contains the number of testcases, T. Next,
T lines follow each containing a long string S.

Output Format For each long string S, display the no. of times SUVO
and SUVOJIT appears in it.


I wrote the following code for this :

#include <bits/stdc++.h>
using namespace std;

int main()
{
int t;
cin >> t;


while (t--) {
int suvo = 0;
int suvojit = 0;
string s;
cin >> s;

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

if (s.substr(i, 7) == "SUVOJIT")suvojit++;
}
for (int i = 0; i <= s.size() - 4; i++) {

if (s.substr(i, 4) == "SUVO")suvo++;
}

cout << "SUVO = " << suvo - suvojit << ", SUVOJIT = " << suvojit << "\n";

}
return 0;
}


The code about gave out of bounds exception for substr() function for this test case:


15
RSUVOYDSUVOJITNSUVOUSUVOJITESUVOSUVOSGSUVOKSUVOJIT
SUVOJITWSUVOSUVOJITTSUVOCKSUVOJITNSUVOSUVOJITSUVOJITSUVOSUVOSUVOJITTSUVOJ
SUVOSUVOSUVOJITASUVOJITGCEBISUVOJITKJSUVORSUVOQCGVHRQLFSUVOOHPFNJTNSUVOJITKSSUVO
SUVOJITSUVOJITJGKSUVOJITISUVOJITKJLUSUVOJITUBSUVOX
MMHBSUVOFSUVOFMSUVOJITUMSUVOJITPSVYBYPMCSUVOJIT
OASUVOSUVOJITSUVOSTDYYJSUVOJITSUVOJITSUVO
RLSUVOCPSUVOJITYSUVOSUVOOGSUVOOESUVOJITMSUVO
WVLFFSUVOJITSUVOVSUVORLESUVOJITPSUVOJITSUVO
RSUVOSUVOJITQWSUVOUMASUVOSUVOJITXNNRRUNUSUVOJIT
HYLSSUVOSUVOSUVOJITPOSUVOJIT DGMUCSSSUVOJITMJSUVOHSUVOCWTGSUVOJIT
OBNSSUVOYSUVOSUVOJITSUVOJITRHFDSUVODSUVOJITEGSUVOSUVOSUVOJITSUVOSUVOJITSSUVOSUVOSUVOSSUVOJIT
AG NSUVOJITSUVOSUVOJIT CGJGDSUVOEASUVOJITSGSUVO


However, when instead of using the s.size() function , I converted the string into a char constant and took the length of it using strlen, then the code caused no error and everything went smoothly ..

So, my question is ...Why did this happen?

This is my working code with the change :

#include <bits/stdc++.h>
using namespace std;

int main()
{
int t;
cin >> t;


while (t--) {
int suvo = 0;
int suvojit = 0;
string s;
cin >> s;
int le = strlen(&s[0]);
for (int i = 0; i <= le - 7; i++) {

if (s.substr(i, 7) == "SUVOJIT")suvojit++;
}
for (int i = 0; i <= le - 4; i++) {

if (s.substr(i, 4) == "SUVO")suvo++;
}

cout << "SUVO = " << suvo - suvojit << ", SUVOJIT = " << suvojit << "\n";

}
return 0;
}

Answer

In one case, you use size_t, in the other case you use int.

If the length is for example 6 characters, then s.size () - 7 is not -1, but one huge number and everything goes wrong. But if you write int len = strlen (...), then len - 7 is indeed -1 and everything is fine.

When I see a number subtracted from size_t, that's an immediate red flag. Write "i + 7 ≤ s.size()", not "i ≤ s.size() - 7".