search in a textfile

Posted on 2004-04-18
Last Modified: 2010-04-15
I have a problem. I have write a small textfile and now i want to do a program that analyses my textfile and gives me the prints out the following information on the screen.

1. how many characters in the textfile.? many lines in the textfile.? many  figurecharacters (0-9) in the textfile.?
4.The character that appears the most in the text and how many times it appears.?

need help
Question by:bosselini
  • 2
  • 2
  • 2
  • +2
LVL 22

Accepted Solution

grg99 earned 168 total points
ID: 10854995
make an array of integers, indexed by char, call it occurrences

open the file
totlines = 0; totchars = 0; fchars = 0;  occurences[ all ] = 0;

while it isnt end of file:
    read character, say c;    add one to totchars;
   if c is EOL, addd one to totlines
   if c is 0..9 then add one tot figurechars

   add one to occurrences[ c ];

LVL 45

Expert Comment

ID: 10856690
Is this your homework ? If yes, then pls post what have you done so far and where do you need help

Expert Comment

ID: 10856805
Just a bit off the homework,here's a small trick to read the number of characters in the file quickly.

<code edited by sunnycoder, Page Editor>

Sunny,pls feel free to edit the code part if you deem necessary.
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.


Author Comment

ID: 10861488
I dont understand ..need more help ..please...

Assisted Solution

Avik77 earned 166 total points
ID: 10862915
   no code just a little bit of  hint . Do a scan character by character over the target file until EOF, and
1. Initialize a counter for line.
2. Initialise an array of integers of length 256 with 0.
  like static int arr[256];
3. when u encounter a character increment the array as (let the character assigned to c) then
arr[(int)c]++ to record the number of repetitions of which chars.
4. when u encounter a newline '\n' increment ur line counter.
5. use the arr[48] .. arr[57] to determine which numbers appeared how many times.
6. Sum up ur array elements . It will be giving u the number of characters.

Best of Luck.
LVL 45

Assisted Solution

sunnycoder earned 166 total points
ID: 10866363

>I dont understand ..need more help ..please...
For us to help you, we need to see that you are really sincere about learning and are not fishing around for code ... I will elaborate the problem and try to explain whatever I can. If you need further assistance then either you need to be very specific about the issue with which you need help or you need to show some real good efforts from your side.

Objective 1. - how many characters in the textfile.?
This should be rather simple to imagine ...
-open the file ...
-read it while keeping count of number of characters read ..
-close the file
A three step solution for which c provides you with very convinient API in the form of fopen fgets and fclose ... Take a look at help/man pages for these functions and you will get an idea of what needs to be done

Objective 2. - how many lines in the textfile.?
If you have already achieved objective 1, then this will be a piece of cake ...
How do you determine the number of lines in the file ? obviously by determining how many newlines are present in the file!!
In C, newline character is identified by the escape sequence '\n' ... So while you were reading the number of characters for objective 1, all you need to is check every character if it is '\n' .... If it is, then you have a new line ....
Keep track of the lines you have seen and by the end of the program thou shall have counted the number of lines :o)

Objective 3. - how many  figurecharacters (0-9) in the textfile.?
If you have followed the discussion until now, then this is easier than ever !!
Just like you compared each character to see if it was a newline, here you need to check if it is a digit .... Any digit character will be smaller than or equal to '9' and larger than or equal to '0' .... Also C provides you with an isdigit() function to determine if a given character is a digit !!

Objective 4. - The character that appears the most in the text and how many times it appears.?
Now this will be some work .... Simplest way will be declare an array of counters for all possible characters ...
For our example let us assume that there will be maximum 10 characters 0-9 (ASCII values) ... we declare an array a[10]
Entry at index i in this aray corresponds to the occurances of the character with ASCII value i ...
For initialization, you need to set all the elements in this array to zero
Whenever we read a character from the file, we increment increment the corresponding entry a[i]++

If this seems difficult at the moment, leave it ... work on 1-3 .. once you have them working, you will feel confident enough to tackle 4 ... incase you get stuck, we are here to help

good luck

Expert Comment

ID: 10871119
>Objective 1
>Objective 3
>Objective 4
Can be solved using 1 single array of integers of size 256 (at least for a text file). Just index into the array with the ascii value of the character read by fgetc and increment the content of that location.
like,declare c and arr[256] as int.
Initialize all the elements of arr[256] to zero, and
c=fgetc(fp); // where c is the character read

This not only solves ur problem in objective 1, i.e when the file has been scanned fully the total of all array elements is ur no of characters, the arr[48] value represents no of 0's appearing in ur text since 48 is the ascii value of character '0', and so on...solving ur objective 3. For objective 4 just find the maximum number in the array , say arr[i] is the maximum. Hence a statement like
printf("%c appears the most with %d times",i,arr[i]);

Most of the Objectives has been solved with just one single data structure. I am only re-stating my previous post since sunnycoder has divided them into distinct *objectives* I tried to fit my previous explanation here.

Please respond to what u have understood so far...


Author Comment

ID: 10871320
Guys thanks for the help i am now trieng to work on it...will seee..i will respond after i have tried.....

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question