Hi there people i have a problem i have been asked to Accept characters from a data file. Convert all the characters to upper case,Save the characters to a new data file which i have done. But for some reason im stuck on trying to get the following to work:
Detect a word
Detect a position of the word
Detect a number of occurrences of the word
couldnt find anything on the internet so i havent got anywhere lol

i may be having an off day so if some one could please explain on how to do this without telling me or maybe some hints and il try and do it my self thanks

here is the code for the 1st part just incase

#include <stdafx.h>
#include <ctype.h>
#include <cstdio>

int _tmain(int argc, _TCHAR* argv[])
  int ch;
  int user;
  FILE *pt;
  FILE *upper;

   pt = fopen( "portfolio2.txt", "r" );
   upper = fopen( "upper.txt", "w" );
   printf("Do You Want To Convert Characters To Uppercase \nPlease Enter 1 For Yes 0 For No\n");
   if (user == 1)
    ch = getc(pt);
    while(ch != EOF)
	 ch = toupper(ch);
     putc(ch, upper);
     ch = getc(pt);
	printf("Converted Please Check The upper.txt\n");
	return 0;

Well, you have three how-to problems:
1. Detect the word: it's the simplest problem - see code stub below
2. Select the word (detect end of word): see code stub below...
3. Detect a number of occurences of every word (make the dictionary): it's much more harder task.
You need to invent two artifacts: a word accumulator and a dictionary. Have a look at a possible main loop stub:

int inword = 0; /* word length */
    /* Open files... */
    /* Initialize the dictionary */
    /* Prepare word accumulator  */
    /* with inword = 0 operation */
    while ((ch=getc(fin)) != EOF) {
        if (isalpha(ch)) {
            ch = toupper(ch);
            /* Append letter to the word */
            /* with ++inword operation...*/
        } else if (inword) { /* end of word */
            /* Look up the dictionary */
            /* with inword = 0 at end */
        } /* else skip non-letter */
        putc(ch,fout); /* copy to output */
    if (inword) { /* Don't forget: */
        /* Process the last word */
    /* Close files... */
    /* Now you have the dictionary... */

The word accumulator is a data structure to accumulate all letters of the current word. The dictionary contains all words with counters. At the end of word you must search accumulated word in the dictionary. If it's a new word, insert it into the dictionary with counter = 1. If the word is in the dictionary, increment its counter in the dictionary node.

There are lots of ways to implement a word accumulator and a dictionary - from the simplest, slow and dangerous to the fast and robust...

This article has been dead for over six months. Start a new discussion instead.