Link to home
Start Free TrialLog in
Avatar of bosselini
bosselini

asked on

search in a textfile

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
ASKER CERTIFIED SOLUTION
Avatar of grg99
grg99

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sunnycoder
Is this your homework ? If yes, then pls post what have you done so far and where do you need help
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.
Avatar of bosselini
bosselini

ASKER

I dont understand ..need more help ..please...
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>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.
Guys thanks for the help i am now trieng to work on it...will seee..i will respond after i have tried.....