Alright, this is a working code after I got some extra help from my classmate:
Code:
#include<iostream>
#include<string>
#include<conio.h>
#include<fstream>
#include<algorithm>
using namespace std;
void bubble(string *dictionary)
{
string temp;
for(int y=0; y<9999; y++)
{
for(int g=0; g<9998; g++)
{
if(dictionary[g]>dictionary[g+1])
{
temp=dictionary[g];
dictionary[g]=dictionary[g+1];
dictionary[g+1]=temp;
}
if(dictionary[g]==dictionary[g+1])
{
dictionary[g]="";
}
}
if(y%100==0)
{
cout<<"Current progress: "<<y<<endl;
cout<<dictionary[y]<<endl;
}
}
}
void output(string *dictionary)
{
ofstream outFile;
outFile.open("append.dat");//, ios::app);
int x=0;
while(x<9999)
{//if(x>2000)
if((dictionary[x]!="") && (dictionary[x]!=" ") )
outFile<<dictionary[x]<<endl; x++;
if(x%100==0)
cout<<x<<endl;
}
outFile.close();
cout<<"done"<<endl;
}
int main()
{
int a=0;
ifstream inFile;
string dictionary[10000], word[10000];
string input, temp;
inFile.open("SDawg.txt");
while(a<9999)
{temp="";
getline(inFile,input);
for(int i=0; i<input.length(); i++)
{
input[i]=tolower(input[i]);
if( (input[i]!=' ') && (input[i]>='a') && (input[i]<='z') )
temp+=input[i];
if(input[i]==' ')
{
if(a<9999)
{
dictionary[a]=temp;
temp="";
cout<<dictionary[a]<<endl;
a++;
}
}
}
}
bubble(dictionary);
output(dictionary);
}
I do have a project that spins off this one. The objective is to pick out words from the append.dat that are pseudo palindrome (that is, moving the first letter to the end of the word and be have the same words backwards as it was forwards. i.e. banana would be ananab which is still banana backwards.
My thoughts on this is just to remove the first character from each line of text and then run a palindrome on it (see working copy below), but I need to create a function that removes the first character of each line before I run palindrome on it (and I shouldn't need to run this through bubble again). What would be the best way to go about this?
Palindrome: (I would not run this as an if/else, just remove the equal algorithm and replace it in the main)
Code:
#include<iostream>
#include<conio.h>
#include<string>
#include<algorithm>
using namespace std;
void pal(const std::string& str)
{
if(std::equal(str.begin(), str.begin() + str.size()/2, str.rbegin()))
{
cout<<"This text is a palindrome"<<endl;
}
else
{
cout<<"This text is not a palindrome"<<endl;
}
}
int main()
{
int i;
string str;
cout<<"Enter a word/words"<<endl;
getline(cin, str);
transform(str.begin(), str.end(), str.begin(), tolower);
for(i=0; i<str.length();)
{
if(str[i]==' '||str[i]==','||str[i]=='\''||str[i]=='?'||str[i]=='!')
{
str.erase(i,1);
}
else
{
i++;
}
}
pal(str);
getch();
}