Skip to content

Instantly share code, notes, and snippets.

@WilliamHester
Last active August 29, 2015 14:16
Show Gist options
  • Save WilliamHester/ee8eaf8af5b9de59e143 to your computer and use it in GitHub Desktop.
Save WilliamHester/ee8eaf8af5b9de59e143 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int main(int argc, char const *argv[])
{
/*Develop Variables*/
FILE *inp; /*file pointer*/
int status; /*fscanf feed back*/
int i = 0; /*counter variable*/
const char *word = argv[2]; /*word found*/
int wordc; /*word count*/
/*check number of argument in command line*/
if (argc < 2)
{ /*when arguments < 2 are stated*/
printf ("EE 233 Spring 2015 P4,Toneyah,Toney A. Hicks");
return -1;
// exit(0);
}
else
{
/*attempt to open file specified*/
inp = fopen (argv[1], "r");
/*if file does not open, error*/
if(inp == NULL)
{
printf("Could not open file. Will close\n");
return -1;
}
else
{
char *string = NULL; // the string that we're going to read in
// guaranteed to have <= 80 characters
size_t length;
while (getline(&string, &length, inp) != -1)
{ // We're good to go.
int wordLength = strlen(word);
// printf("%d\n", length);
int j;
int notspaces = 0;
for (j = 0; j < strlen(string); j++)
{
if (!isspace(string[j]))
{
notspaces++;
}
}
if (notspaces == 0) continue;
for (int j = 0; j < length - wordLength; j++)
{
int k;
for (k = 0; k < wordLength; k++)
{
if (string[j + k] != word[k])
{
break;
}
}
if (k == wordLength)
{
// We found an occurance of the specified word
i++;
}
}
}
printf("%s contains %.4d total words.\n", argv[1], i);
}
}
return (EXIT_SUCCESS);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment