Codeforces String Task Need Help

Can anyone tell me why my test case gives SIGSEV when input is
femOZeCArKCpUiHYnbBPTIOFmsHmcpObtPYcLCdjFrUMIyqYzAokKUiiKZRouZiNMoiOuGVoQzaaCAOkquRjmmKKElLNqCnhGdQM
The problem is String Task
Here is my implementation:

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

int main() {
	// your code goe
	string s1;
	string s2 = "";
	cin>>s1;
	int c = 0;
	int len = s1.length();
	for(int i=0;i<len;i++)
	{
	    s2[i] = tolower(s1[i]);
	    if(s2[i] == 'a' || s2[i] == 'e' || s2[i] == 'i' || s2[i] == 'o' || s2[i] == 'u' || s2[i] == 'y')
	        continue;
	    else
	        cout<<'.'<<s2[i];
	}
	return 0;
}

The reason why your code is giving TLE is because you have a string named s2 that is empty and you want to assign s2[i] = (some character) where i belongs to [0,1,…,len-1]. This basically means you want to write at some memory location that your program doesn’t have access to write. So what you need to do is instead of string s2 you can make a char variable and it will be fine or alternatively you can resize s2 to len by s2.resize(len); then also it will be fine.

1 Like