All pastes #239119 Raw Edit

Anonymous

public text v1 · immutable
·published 2006-11-05 06:16 UTC
rendered paste body
#include <iostream>
#include <fstream>

using namespace std;

const int MAX_WORDS = 10000;
const int MAX_WORD_LENGTH = 25;

bool goodLetter(char a);
void selectionSort(char words[][MAX_WORD_LENGTH], int n);

int main (int argc, char *argv[])
{
        char current, currentprev;
        int i = 0, j = 0, k = 0;
        bool twolines = 0;

        char words[MAX_WORDS][MAX_WORD_LENGTH];
        char temp[MAX_WORD_LENGTH];


        ifstream in_stream;
        in_stream.open(argv[1]);

        while(!in_stream.eof())
        {
                in_stream.get(current);
                current = tolower(current);

                if((current == ' ' || current == '\n') && twolines == 1) // Move to next line of words[][] if there is a space in input
                {
                        while(!goodLetter(words[i][strlen(words[i])-1]))
                        {
                                words[i][strlen(words[i])-1] = '\0';
                        }
                        i++;
                        j = 0;
                        twolines = 0;
                }

                if(j == 0 && goodLetter(current) || j != 0)
                {
                        words[i][j] = current;
                        j++;
                        twolines = 1;
                }
                current = currentprev;
        }

        selectionSort(words,i);

        for(int x = 0; x < i; x++)
        {
                for (int y = 0; y < MAX_WORD_LENGTH; y++)
                {
                        cout << words[x][y];
                }
                cout << endl;
        }
        return 0;
}

bool goodLetter(char a)
{
        if((a >= 'a' && a <= 'z') || (a >= '0' && a <= '9'))
        {
                return 1;
        }
        else
        {
                return 0;
        }
}

void selectionSort(char list[][MAX_WORD_LENGTH], int n)
{
        for(int i = 0; i < n - 1; i++)
        {
                int smallest = i;
                for(int j = i+1;j < n; j++)
                {
                        if(strcmp(list[j],list[smallest]) < 0)
                        {
                                smallest = j;
                        }
                }
                char temp[1][MAX_WORD_LENGTH];
                strncpy(temp[0],list[i],MAX_WORD_LENGTH);
                strncpy(list[i],list[smallest],MAX_WORD_LENGTH);
                strncpy(list[smallest],temp[0],MAX_WORD_LENGTH);
        }
        return;
}