In: Computer Science
Hi! I wrote two function to get count cluster of char in a string , charFreq(), and an other one which iterate through a vector line by line looking for last element on each line, last(). The problem is, after appending all chars in a string and try to count clusters of values in that string, I get seg fault. I feel like my logic behind is ok, but I am not sure what I did wrong. Can someone help and tell me where are my errors. Also, all computation are done on txt file.
-----------------------------------------------------------------------------:c++ program
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <fstream>
using namespace std;
//func to check frequency of a char in a string
void charFreq(string& Mystring){
int w = 1;
vector<int> count; //store all freq int into a vector
for(int i = 0; i <= Mystring.length(); i++){
if(Mystring.at(i)== Mystring.at(i+1))//if two adjacent value are the same increment my w: aaa = 3a
{
w++;
count[i]=w;
}
}
//print out all int charFreq.
for(int x = 0; x < count.size()-1; x++){
cout << count[x]<<" ";
}
}
void printArray(vector<string> array) {
for (int i = 0; i < array.size() - 1; ++i)
cout << array[i] << endl;
}
//iterate through vector looking for last char and append all chars into one string
string last(vector<string>& line){
char w;
string x;
string y = " ";
for(int i = 0; line.size(); i++){
w = x.at(x.length()-1);//find last char in a string
y += w;//append my char to a string
}
return y;//return entire string of appended chars
}
int main(int argc, char *argv[])
{
string i;
string input_line;
vector<string> value;
ifstream infile("Freq.txt");
if (infile.is_open())
{
while (getline(infile, input_line))
{
for (int i = 0; i < input_line.size(); i++)
{
value.push_back(input_line);//push individual input_line into vector value
charFreq(input_line);
printArray(value);
last(value);
}
}
infile.close();
}
return 0;
}
------------------------------------------------------------------------------------------------Freq.txt file
there!Hi
!Hi there
Adios
Hi there!
diosA
e!Hi ther
ere!Hi th
here!Hi t
i there!H
iosAd
osAdi
re!Hi the
sAdio
---------------------------------output
1i 1e 1s 1!.........................1o
/*
you did wrong in for loop where "i=Mystring.length()"
when i=Mystring.length() and you write Mystring.at(i+1) which is
illegal memory access/
prompt to segmentation fault.
*/
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <fstream>
using namespace std;
//func to check frequency of a char in a string
void charFreq(vector<char> & Mystring)
{
int hash[128]={};
//store all freq int into a vector
for(int i = 0; i <Mystring.size(); i++)
{
hash[Mystring.at(i)]++;
for(int j=i+1;j<Mystring.size();j++)
{
if(Mystring.at(i)==Mystring.at(j))
{
hash[Mystring.at(i)]++;
}
}
}
for(int i=0;i<128;i++)
{
if(hash[i]>=1)
cout<<(char)i<<" "<<hash[i]<<endl;
}
}
void printArray(vector<char> &array) {
for (auto i = array.begin(); i != array.end(); ++i)
cout << *i ;
cout<<endl;
}
char last(string &line){
if(line.length()>0)
return line.at(line.length()-1);//return entire string of appended
chars
return '~';
}
int main(int argc, char *argv[])
{
string i;
string input_line;
vector<char> value;
ifstream infile("Freq.txt");
if (infile.is_open())
{
while (infile)
{
getline(infile, input_line);
if(input_line.length()>0)
value.push_back(last(input_line));//push individual input_line into
vector value
}
printArray(value);
charFreq(value);
infile.close();
}
return 0;
}