vendredi 23 février 2018

How can I rewrite my if-statements to work?

So what the program should basically do is we input filename -i input.txt -o output.txt -c in the command line (command line arguments (argc and argv)) and the program should read the inputfile, count how many words there are, and if there is -c it should convert all of uppercases to lowercases and ignore punctuation. And then count the occurrences of every word and output the result in the ouput.txt. If there is no -i input.txt it should prompt for input from user and do the same and if there is no -o output.txt it should print the output in the compiler. So what's not working from my program is if(argc == 4), if(argc == 3). I don't have the function for wordOccurrences for now. I know that the header "count.h" should't be like a header but for now it works.

MAIN FILE

#include "count.h"
#include <stdio.h>``
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include "functions.c"

int main(int argc, char **argv)
{   
    //Initialize variables
    FILE *fp; //file        
    char buffer[1000];

    char *input; //for manually entering string with -c
    input = (char*)malloc(100 * sizeof(char));

    int wordcount; // the number of words

    int *ch; //a single character
    ch = (int*)malloc(100000 * sizeof(int));

    char consoleInput[60];

    if(argc >=2)
    {       
        if(argc == 6) //if argc = 5 --> wordcount -i .txt -o .txt -c
        {       
            //for (int i=0; i<=argc; i++)
            //{ 
            // if line has -i
            if (strcmp("-i", argv[1]) == 0)
            {
                //open file on directory after -i
                fp = fopen(argv[2],"r");

                //do wordcount method
                wordcount = countForFile(fp, wordcount);

                //close file
                fclose(fp);
            }// end of first if

            // if line has -o
            if(strcmp("-o", argv[3]) == 0)
            {
                //open file on directory after -o
                fp = fopen(argv[4], "w");

                //put wordcount in file
                fprintf(fp,"%d", wordcount);
                printf("Putting output to file\n");

                //close file
                fclose(fp);
            } // end of second if

            //if line has -c
            if(strcmp("-c", argv[5]) == 0)
            {
                //open file on location 2
                fp = fopen(argv[2],"r");

                // do toLowerCase method
                toLowerCase(fp,ch);

                //open file on location 2
                fp = fopen(argv[2],"r");

                //count occurances

                printf("Well countOccurances is in process\n");

                //close file
                fclose(fp);

            } //end of third if

            //}
        }
        if (argc == 5) // if argc = 4 --> wordcount -i .txt -o .txt 
        {
            //for (int i=0; i<=argc; i++)
            //{ 
            // if line has -i
            if (strcmp("-i", argv[1]) == 0)
            {
                //open file on directory after -i
                fp = fopen(argv[2],"r");

                //do wordcount method
                wordcount = countForFile(fp, wordcount);

                //close file
                fclose(fp);
            }// end of first if

            // if line has -o
            if(strcmp("-o", argv[3]) == 0)
            {
                //open file on directory after -o
                fp = fopen(argv[4], "w");

                //put wordcount in file
                fprintf(fp,"%d", wordcount);
                printf("Putting output to file\n");

                //close file
                fclose(fp);

            } // end of second if
            //}
        }
        else if(argc == 4) // if argc = 3 -> wordcount -i txt -c; wordcount -o txt -c
        {
            //for (int i=0; i<=argc; i++)
            //{
            if(strcmp("i",argv[1]) == 0)
            {
                printf("first i if working\n"); 
                if(strcmp("i",argv[1]) == 0)
                {
                    //open file on directory after -i
                    fp = fopen(argv[2],"r");

                    //do wordcount method
                    wordcount = countForFile(fp, wordcount);

                    //close file
                    fclose(fp);
                    printf("I if working\n");
                }
                //if line has -c
                if(strcmp("-c", argv[3]) == 0)
                {
                    //open file on location 2
                    fp = fopen(argv[2],"r");

                    // do toLowerCase method
                    toLowerCase(fp,ch);

                    //open file on location 2
                    //fp = fopen(argv[2],"r");

                    //count occurances

                    printf("Well countOccurances is in process\n");

                    //close file
                    fclose(fp);

                } //end of third if
            }
            else if(strcmp("-o",argv[1]) == 0)
            {
                printf("Enter q to exit\n");
                printf("Enter text: ");
                wordcount = count(input, wordcount);

                if(strcmp("-o", argv[1]) == 0)
                {
                    //open file on directory after -o
                    fp = fopen(argv[2], "w");

                    //put wordcount in file
                    fprintf(fp,"%d", wordcount);
                    printf("Putting output to file\n");

                    //close file
                    fclose(fp);
                } // end of second if

                //if line has -c
                if(strcmp("-c", argv[3]) == 0)
                {
                    //open file on location 2
                    fp = fopen(argv[2],"r");

                    // do toLowerCase method
                    toLowerCase(fp,ch);

                    //open file on location 2
                    fp = fopen(argv[2],"r");

                    //count occurances

                    printf("Well countOccurances is in process\n");

                    //close file
                    fclose(fp);

                } //end of third if
            }
            //}
        }   

        if(argc == 3) // if argc = 2 -> wordcount -i txt; wordcount -o txt
        {
            //for (int i=0; i<=argc; i++)
            //{
            if(strcmp("i",argv[1]) == 0)
            {
                //open file on directory after -i
                fp = fopen(argv[2],"r");

                //do wordcount method
                wordcount = countForFile(fp, wordcount);

                //close file
                fclose(fp);
                printf("PLS WORK\n");
            }   
            else if(strcmp("-o", argv[1]) == 0)
            {
                printf("Enter q to exit\n");
                printf("Enter text: ");
                wordcount = count(input, wordcount);

                //open file on directory after -o
                fp = fopen(argv[2], "w");

                //put wordcount in file
                fprintf(fp,"%d", wordcount);
                printf("Putting output to file\n");

                //close file
                fclose(fp);
            } // end of second if
            //}
        }
        if(argc == 2) //wordcountMain -c
        {
            //for (int i=0; i<argc; i++)
            //{
            //converting from UpperCase to LowerCase
            if(strcmp("-c", argv[1]) == 0)
            {   
                //Get input
                printf("Enter text: ");
                fgets(input,1000,stdin);

                int i = 0;
                //Loop through input
                for( i = 0;input[i]!='\0'; i++) 
                {
                    //find upperCase letters
                    if(input[i] >= 'A' && input[i] <= 'Z')
                    {
                        //overwrite to lowerCase
                        input[i] = tolower(input[i]);

                        //input[i] = input[i] +32;
                        printf("Converted upperCase to lowerCase\n");

                    }//end of if statement  

                    //ignoring punctuation  
                    if(input[i] == ',' || input[i] == '.' || input[i] == '!' || input[i] == '?' || input[i] == '"' || input[i] == ':' || input[i] ==';' || input[i] == '-')
                    {
                        input[i] = ' ';
                        printf("Converted without punctuation\n");
                    }
                } //end of for loop

                printf("%s",input);
            }
            //}
        }
    }
    else
    {
        //Get input
        printf("Enter text: ");
        //fgets(input,1000,stdin);
        wordcount = count(input, wordcount);
        printf("%s",input);
        wordcount = count(input, wordcount);
    }
    return 0;
}

count.h

# pragma once
// This file needs to know what printf is
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>

//count how many words for read file
int countForFile(FILE *file, int wordcount)
{
    int ch;
    int linecount, charcount;


     // Initialize counter variables
     linecount = 0;
     wordcount = 0;
     charcount = 0;


   // If file opened successfully, then write the string to file
   if ( file )
   {
       //Repeat until End Of File character is reached. 
       while ((ch=getc(file)) != EOF) {
          // Increment character count if NOT new line or space
            if (ch != ' ' && ch != '\n') { ++charcount; }

          // Increment word count if new line or space character
           if (ch == ' ' || ch == '\n') { ++wordcount; }

          // Increment line count if new line character
           if (ch == '\n') { ++linecount; }

        }
    }
   else
    {
        printf("Failed to open the file\n");
    }
    printf("Words : %d \n", wordcount);

    getchar();
    return wordcount;
}

/*
    Hello, Bye, Hello;

    int index = 2;
    String[] words = [Hello, Bye, null];
    int [] count = [2, 1, 0,];
*/
int count(char *input, int wordcount)
{   
    wordcount = 0;
    while(scanf("%s",input) != EOF)
                {
                    if(input[0] == 'q' && strlen(input) == 1)
                        break;
                    wordcount++;

                }// end of while loop

                printf("WordCount is: %d\n", wordcount);
    getchar();
    return wordcount;
}

function.h

#include "count.h"
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>

//Check for UpperCase and turn into LowerCase for a read file
toLowerCase(FILE *fp,int *ch)
{
    //Initialize variables
    int single_character;
    int i =0;

    //if file is read
    if ( file )
    {
       //Repeat until End Of File character is reached. 
       while ((single_character=getc(fp)) != EOF)
        {
            //check every single character if it's a upperCase
            if(single_character >= 'A' && single_character <= 'Z')
            {
                //if it is overwrite it to lowerCase
                single_character = tolower(single_character); //overwrite
            }   
            ch[i] = single_character;
            i++;
        } //end while   
    } //end if

   //if file not opened 
   else
    {
     printf("Error: File not opened\n");  
    } //end else    

    getchar();

    printf("toLowerCase Working\n");

} // end toLowerCase

So Yeah i would appreciate any help. I am new to C and if you have another idea for how i should make the program work post your ideas. Anything would help really.

Aucun commentaire:

Enregistrer un commentaire