• C

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
Who is Participating?
grg99Connect With a Mentor Commented:
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 ];

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.
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

bosseliniAuthor Commented:
I dont understand ..need more help ..please...
Avik DasguptaConnect With a Mentor Commented:
   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.
sunnycoderConnect With a Mentor Commented:

>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
Avik DasguptaCommented:
>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...

bosseliniAuthor Commented:
Guys thanks for the help i am now trieng to work on it...will seee..i will respond after i have tried.....
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.