Hi fellow programmers. I'm quite new to 'C' and I’ve been working on this personal project recently and have struck a spanner in the works! I am quite bemused by this piece of code I am to assemble (below). I think it’s fairly simple but can’t quite grasp it! Any guidance/code that refers to a program that counts lines and words in the ‘C’ language would be really helpful, here’s the problem…

I need to use emacs to compose an algorithm for counting the number of lines and words in a text file.


The words may comprise of any printing characters and are delimited by spaces and/or tabs.


Also the presence of any control character other than tab, eol (end-of-line), or a single terminating eod (end-of-data), I have considered an error, whereupon the algorithm should terminate.


I will need to give two levels of pseudocode before I create the program.


I then need to enclose my solution to in a comment block and implement my algorithm in C. I need to include comments with your code and I can’t define any other function than main.


Finally I need to devise a test file that serves to adequately test my program.

If anyone can help I will be eternally gratefull!
Thanks again
Tim

Recommended Answers

All 5 Replies

I need to use emacs to compose an algorithm for counting the number of lines and words in a text file.


The words may comprise of any printing characters and are delimited by spaces and/or tabs.


Also the presence of any control character other than tab, eol (end-of-line), or a single terminating eod (end-of-data), I have considered an error, whereupon the algorithm should terminate.


I will need to give two levels of pseudocode before I create the program.


I then need to enclose my solution to in a comment block and implement my algorithm in C. I need to include comments with your code and I can’t define any other function than main.


Finally I need to devise a test file that serves to adequately test my program.

Heh. This looks like a school assignment with the word YOU replaced with I.

the word YOU replaced with I.

He he that's a nice one.. :)

OK, well its not really a school project, but it is a project.

Ive got as far as to get the program counting words but dont know how to get it to count and display the lines alongside the wordcount.

Here's the code so far...

#include <stdio.h>
#include <string.h>
#define SIZE 100

int count(char vector[],int nr);

int main()

{  
     int i,string_size,x; 
     char string[SIZE]; 
     printf("Type a text: ");
     gets(string);
     string_size=strlen(string);
     x=count(string,string_size);
     printf("Number of words: %d\n",x);

     return 0;
}

int count (char vector[],int nr)

{
    int gasit=0,i; 
    for(i=0;i<nr;i++)
    {
        if(vector[i]>='a' && vector[i]<='z') gasit++;
          if(vector[i]>='A' && vector[i]<='Z') gasit++;
    }

    return gasit;

    }

Any help would be apprieciated,

Thanks
Tim

What you're counting is number of alphabets in the string not the words.
Assuming your user inputs a string which does not contain more than one space between each word, number of words in that string would be = number of spaces.
Once you've implemented this much you can consider other cases:
- If the string ends with a word then there won't be a space after last word. So ++wordCount.
- If there are variable number of spaces between words you'll have to skip them 'appropriately' (loop till you reach non-space char).
- If you're allowing users to put any other type of white space char (like tab '\t') then take care of that too.
and teh list goes on, you can decide how far you wanna go..

>OK, well its not really a school project, but it is a project.
What kind of a newbie wants to write 2 levels of pseudocode before they create their project? :eek:

Regardless, thekashyap is correct, and you should do what he suggests. Another thing you should stop doing is gets.

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.