Solved

search in a textfile

Posted on 2004-04-18
11
347 Views
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.?
2.how many lines in the textfile.?
3.how 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
0
Comment
Question by:bosselini
  • 2
  • 2
  • 2
  • +2
11 Comments
 
LVL 22

Accepted Solution

by:
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 ];

0
 
LVL 45

Expert Comment

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

Expert Comment

by:ankuratvb
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.
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

Author Comment

by:bosselini
ID: 10861488
I dont understand ..need more help ..please...
0
 
LVL 2

Assisted Solution

by:Avik77
Avik77 earned 166 total points
ID: 10862915
Hi,
   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.
Avik.
0
 
LVL 45

Assisted Solution

by:sunnycoder
sunnycoder earned 166 total points
ID: 10866363
bosselini,

>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
sunnycoder
0
 
LVL 2

Expert Comment

by:Avik77
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
...
...
arr[c]++;

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

Avik.
0
 

Author Comment

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

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

821 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