can someone please identify why I am getting segmentation fault in this code?
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
#include <algorithm>
using namespace std;
int main(int argc, char *argv[]) {
ifstream stream(argv[1]);
string str;
while(getline(cin,str,'\n'))
{
int sum = 0;
int length = str.length();
for(int x = 0; x < length; x++)
str[x] = tolower(str[x]);
int arr[26] = {0};
int index = 0;
for(int x = 0; x < length; x++)
{
index = str[x] - 97;
arr[index]++;
}
sort(arr,arr+26);
int num = 26;
for(int x = 25; x >= 0; x--)
{
sum = sum + (arr[x]*num);
num--;
}
cout << sum << endl;
}
return 0;
}
it is running completely fine on my pc’s compiler, i dont know why codeeval(yes this is for a codeeval problem) doesnt accept this code
PS
i am getting the same answer from the test cases. but my question is about the segfault