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");
   scanf("%d",&user);
   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");
   }
     fclose(pt);
     fclose(upper);
	return 0;
}

Dani AI

Generated

You already have the uppercase conversion done (nice work, ). 's outline is the right idea: treat a word as a sequence of letters, accumulate the letters, then lookup/insert in a dictionary at word end. The practical gaps most people hit next are (a) recording where the word started, (b) choosing a dictionary structure that lets you update a counter and keep a list of positions, and (c) handling memory and locale safely. The guidance below fills those gaps and gives a small, safe C data-layout you can apply immediately.

Do the scan once and keep a few running counters: a byte offset (0,1,2...), a line number, and a column. When the first letter of a word is seen record that offset/line/column as the word start, then append letters to a small dynamic buffer until the word ends. Use isalpha((unsigned char)ch) and toupper((unsigned char)ch) to avoid undefined behavior on negative char values (see the C library docs for details: https://en.cppreference.com/w/c/string/byte/isalpha and https://en.cppreference.com/w/c/string/byte/toupper).

Store words in a hash table keyed by the word string. For C, a lightweight option is uthash; it makes lookup/insert trivial and avoids you reimplementing a hash table: https://troydhanson.github.io/uthash/. Each hash entry should hold the word, a counter, and a small dynamic array of positions (or the first N positions if you want to limit memory). Example layout:

struct PosList { size_t *pos; size_t n, cap; };

struct WordEntry {
    char *word;           /* key (strdup) */
    size_t count;
    struct PosList positions;
    UT_hash_handle hh;    /* for uthash */
};

static void poslist_push(struct PosList *pl, size_t p) {
    if (pl->n == pl->cap) {
        pl->cap = pl->cap ? pl->cap * 2 : 4;
        pl->pos = realloc(pl->pos, pl->cap * sizeof *pl->pos);
    }
    pl->pos[pl->n++] = p;
}

Practical cautions: storing every position can use lots of RAM for large texts. Options: keep only count and the first K positions, stream results to disk as you find them, or do a two-pass approach (first pass: counts only; second pass: record positions for words you care about). Also decide what “word” means (letters only? include digits or apostrophes?) before tokenizing. Finally, free all allocations at the end. This should let you implement detection, position tracking, and occurrence counting robustly and efficiently.

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...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.