AbuDavid
AbuDavid

Reputation: 35

Check special character in string array using isalpha

So I am trying to take words from a file and save them into a dynamic array, and then print the array sorted alphabetically along with the concordance of each word in the array. I am facing a problem in identifying special characters/ numbers and deleting them from the array. I want to use the isalpha function as it is in the assignment prompt. When I run my code, it works perfectly but special characters and numbers are not eliminated from the array. Any idea on how to do this using isalpha?

#include <iostream>
#include <stream>
#include <iomanip>
#include <cstdlib>
#include <algorithm>
#include <cctype>
#include <string>

using namespace std;

void loadData();
void checkSpecialCharacters(string wordPtr, int size);
void alphabeticalSort(string *wordPtr, int size);
void frequencyCounter(string *wordptr, int size);


int main()
{
loadData();
return 0;
}

void loadData()
{
string fileName;
cout << "This program processes any text file to give you the concordance for each word present, and how many times it appears in the file." << endl;
cout << "Please enter the name of the text file you want to process followed by '.txt': " << endl;
cin >> fileName;

ifstream dataFile(fileName);
if (dataFile.fail())
{
    cerr << fileName << " could not be opened." << endl; //error message if file opening fails
    exit(-1);
}
string word;
int size = 0;
while (dataFile >> word)
{
    size++;
}
dataFile.clear();
dataFile.seekg(0);

string* wordPtr = new string[size];

int ctr = 0;
for (int i = 0; i < size; i++)
{
    dataFile >> wordPtr[i];
    checkSpecialCharacters(wordPtr[i], size);
    std::transform(wordPtr[i].begin(), wordPtr[i].end(), wordPtr[i].begin(), ::tolower);
    cout << wordPtr[i] << endl;
    
}
    dataFile.close();
    alphabeticalSort(wordPtr, size);
    frequencyCounter(wordPtr, size);
    delete[] wordPtr;
}

void checkSpecialCharacters(string wordPtr, int size)
{
for (int i = 0; i < size; i++)
{
    if (isalpha(wordPtr[i]) == true)
    {
        for (int j = 0; j < size; j++)
        {
            wordPtr[j] = wordPtr[j + 1];
            cout << wordPtr[j];
        }
    }
}
}



void alphabeticalSort(string *wordPtr, int size)
{
int i, j;
string temp; //temporary holding variable

for (i = 0; i < (size - 1); i++)
{
    for (j = 0; j < (size - 1); j++)
    {
        if ((wordPtr[j])>(wordPtr[j + 1]))
        {
            temp = wordPtr[j];
            wordPtr[j] = wordPtr[j + 1];
            wordPtr[j + 1] = temp;
        }
    }
}
}
void frequencyCounter(string *wordPtr, int size)
{
    string finalFileName;
    cout << "Please enter the name of the file that you want to store the concordance in followed by .txt: " << endl;
    cin >> finalFileName;
    ofstream concordanceFile(finalFileName, ios::out);
    if (concordanceFile.fail())
    {
        cerr << finalFileName << " could not be opened." << endl;
        exit(-1);
    }
    int frequency = 1;
    int index = 1;
    string element = wordPtr[0];
    while (index < size)
    {
        if (wordPtr[index - 1] == wordPtr[index]) // check if element is equal to previous element
        {
            frequency++;
            index++;
        }
        else
        {
            concordanceFile.setf(ios::left);
            concordanceFile << setw(10) << element << " " << setw(10) << frequency << endl;
            cout.setf(ios::left);
            cout << setw(10) << element << " " << setw(10) << frequency << endl;
            element = wordPtr[index];
            index++;
            frequency = 1; //reset frequency
        }
    }
    cout << "Concordance data saved in " << finalFileName << " successfully!" << endl;
}

Upvotes: 1

Views: 122

Answers (0)

Related Questions