1

This is what I have come up with so far.

#include<stdio.h>
main()
{
int w=0, v=0, c=0, cnt=0;
char inp[21]="abcd aeiou hi there", ch;
FILE *read, *write;

write = fopen("D:/wordvowelcharacter.txt", "w");
fprintf(write, "%s", inp);

fclose(write);

read = fopen("D:/wordvowelcharacter.txt", "r");

if (read==NULL)
{
    printf("Error opening file");
}

while ((ch=fgetc(read))!=EOF)
{
    if (ch!=' ')
    {
        c++;
    }

    if          (ch=='A'||ch=='a'||ch=='E'||ch=='e'||ch=='I'||ch=='i'||ch=='O'||ch=='o'||ch=='U'||ch=='u')
    {
        v++;
    }

    if (ch==' ')
    {
        w++;
    }

}
printf("Character %d Vowel %d Word %d", c, v, w);

}

--END OF CODE--

The last if statement is of incrementing the word count. What condition should I put there?The current condition gives me wrong number of words, i.e, number of spaces only. The text in the file is : "abcd aeiou hi there"

durron597
  • 31,968
  • 17
  • 99
  • 158
iG0tB00ts
  • 11
  • 1
  • 1
  • 2

4 Answers4

1

If there are no additional requirements or caveats (e. g. any whitespace character is allowed and not only ' ', consecutive whitespace characters may be allowed, etc.), then the formula is overly simple: the number of words is the number of spaces plus one.

  • That works well and fine.. But if I type a single character, the character is taken as a word. So do I need an array to set the condition ch>2? Or is there another way.. I'd prefer not to use an array.. – iG0tB00ts May 10 '13 at 18:09
  • @iG0tB00ts No. If you type one character, then there are 0 spaces, so this works, correctly giving 1 for the number of words. Think about it. –  May 10 '13 at 18:13
  • No as in, I mean.. Say if the text in the file is -"word1 word2 w word3"- then the single 'w' will be taken as a word as there is a space. I would like a word to be counted only if it is more than 2 characters. – iG0tB00ts May 10 '13 at 18:17
  • 2
    Ah, this is just like the real world -- the requirements are never fully specified. – Hot Licks May 10 '13 at 18:20
  • @iG0tB00ts Then you write code that counts one-letter entities and then subtracts that from the result. –  May 10 '13 at 18:33
  • @iG0tB00ts however, there are words with 1 letter only in multiple languages, including English. – Rad'Val May 10 '13 at 23:24
1

I see a couple of issues with your implementation. First, you assume anything not a space is an alphabetic character. What about tabs, newlines, punctuation, etc? Secondly if two words are separated by just a newline, your code won't pick that up, since it only checks for words that are space delimited.

The ctype.h header provides useful functions for determining if a character is whitespace, alphanumeric, punctuation, etc. See GNU C Manual - Classification of Characters for more info. Something like the following should produce more robust results.

Taking into account your comments in other posts that require a word to be more than two characters, the code becomes:

#include <stdio.h>
#include <ctype.h>

int main()
{
  int w=0, v=0, c=0, cnt=0;
  int inword = 0;
  char *inp = "hi there, w w w here's\nmore than\none line.\nAnd contractions and punctuation!";
  char ch;
  FILE *read, *write;

  write = fopen("character.txt", "w");
  fprintf(write, "%s", inp);

  fclose(write);

  read = fopen("character.txt", "r");

  if (read==NULL)
  {
    printf("Error opening file");
  }


  while ((ch=fgetc(read))!=EOF)
  {
    if (isspace(ch))
    {
      if (inword > 2)
      {
        w++;
      }
      inword = 0;
    }
    else if (isalpha(ch) || ispunct(ch)) {
      inword++;

      if (isalpha(ch))
      {
        c++;
        if (ch=='A'||ch=='a'||ch=='E'||ch=='e'||ch=='I'||ch=='i'||ch=='O'||ch=='o'||ch=='U'||ch=='u')
        {
          v++;
        }
      }
    }
  }

  if (inword > 2) w++;

  printf("Character %d Vowel %d Word %d\n", c, v, w);

  return 0;
}
mshildt
  • 8,782
  • 3
  • 34
  • 41
  • What about short words (eg. "a")? – Neil Townsend May 10 '13 at 20:55
  • Original asker specified in the comments of another answer that he only wanted to count words that were greater than two characters long. I agree that a more comprehensive solution would account for short words like "a" and "an". Would probably have to check against a list of known short words to differentiate between valid ones and random letter. Guess it depends on what the intent of the program is. – mshildt May 11 '13 at 00:11
0

Assuming that your string never starts with a space, it would be simplest just to add increment your w by 1.

0
enum status { out, in };
...
    enum status stat = out;
...
    while ((ch=fgetc(read))!=EOF){
        if (ch!=' '){
            if(stat == out)w++;
            stat = in;
            c++;
        }

        if(ch=='A'||ch=='a'||ch=='E'||ch=='e'||ch=='I'||ch=='i'||ch=='O'||ch=='o'||ch=='U'||ch=='u')
        {
            v++;
        }

        if (ch==' '){
            stat = out;
        }
    }
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70