6 Replies - 370 Views - Last Post: 07 January 2012 - 09:45 AM Rate Topic: -----

#1 theonegandalf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 20-November 11

Question about an exercise in C

Posted 06 January 2012 - 06:41 AM

Guys i need your help. I have an exercise in C.

Quote

It says that the user have to give as an input a text of english language. And then the user has 2 choices
1)chooses one word and the program prints out how much times this word appears in the text.
2)chooses one word and the program prints out in how many different lines of the text this word appears.

It also says : Consider as an end of input the character EOF and that one word is defined as a consecutive sequence of alphabetical characters that do not exceed the length of 100. The recognition of a word should be implemented on a new function.


First of all, observing the 2nd question, i think that i have to store the text in a 2d array. And that each row of the array should represent a row of the text. Is that right?

My problem is that, i do not know the length of the text, so how can i store it inside a dynamical array? I cannot use malloc :/

Is This A Good Question/Topic? 0
  • +

Replies To: Question about an exercise in C

#2 Adak  Icon User is offline

  • D.I.C Addict

Reputation: 245
  • View blog
  • Posts: 805
  • Joined: 01-April 11

Re: Question about an exercise in C

Posted 06 January 2012 - 06:50 AM

So do it line by line, of course. fgets() to the rescue!
Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg  Icon User is offline

  • member icon

Reputation: 3044
  • View blog
  • Posts: 9,280
  • Joined: 25-December 09

Re: Question about an exercise in C

Posted 06 January 2012 - 06:57 AM

You should not need dynamic memory for this program.

Quote

and that one word is defined as a consecutive sequence of alphabetical characters that do not exceed the length of 100.


You know the length of the largest possible word, so use that for your C-string length. Then loop through your file, word by word, checking to see if the entered word matches the word from the file. For the second option it may be easier to read the file line by line into a large buffer then check how many times the entered word appears in that buffer.

Jim
Was This Post Helpful? 0
  • +
  • -

#4 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1831
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Question about an exercise in C

Posted 06 January 2012 - 07:44 AM

First, you should recognize that you don't have to write two different counting functions. Write a single function that reads the entire file and counts both
(1) how many times did the search word appear, and
(2) on how many lines the word appeared

Then you just have to choose what kind of output to print based on the user's choice.

You need two counter variables, right? And for the second task you need one more thing. I'll get back to that later...

The biggest problem is that you don't know the maximum length of a line in the input file. A line doesn't end until there is a newline character, and that can appear after one word, or not at all. In other words, a file containing 20000 words can all be one line. So unless you create a HUGE buffer big enough to hold the biggest conceivable file (and since that's also unknown, the buffer might still not be big enough), you can't count on being able to read the file "line by line".

So a better idea is to read one char at a time into a buffer, continuing until you find a non-alphabetic character. Then 'terminate' the string in the buffer, do your comparison and update counts as necessary.
Then resume reading.

A 100 char buffer is not sufficient since your definition of a word is that it can be up to 100 chars -- so how big do you think it should be?

Now back to the line issue...
you know what signals the end of a line, right? Think about it. So keep track of those to determine when each line ends. Now you can think about what you can use to keep track of whether the search word appeared on each particular line, so that each time you reach the end of a line you'll know whether or not to increment the line counter. Think about that; I don't want to give the whole thing away.
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5667
  • View blog
  • Posts: 22,509
  • Joined: 23-August 08

Re: Question about an exercise in C

Posted 06 January 2012 - 09:48 AM

Perhaps my blog entry here might be helpful?
Was This Post Helpful? 1
  • +
  • -

#6 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 255
  • View blog
  • Posts: 1,661
  • Joined: 20-September 08

Re: Question about an exercise in C

Posted 06 January 2012 - 02:46 PM

View Posttheonegandalf, on 06 January 2012 - 08:41 AM, said:

Guys i need your help. I have an exercise in C.

Quote

It says that the user have to give as an input a text of english language. And then the user has 2 choices
1)chooses one word and the program prints out how much times this word appears in the text.
2)chooses one word and the program prints out in how many different lines of the text this word appears.

It also says : Consider as an end of input the character EOF and that one word is defined as a consecutive sequence of alphabetical characters that do not exceed the length of 100. The recognition of a word should be implemented on a new function.


First of all, observing the 2nd question, i think that i have to store the text in a 2d array. And that each row of the array should represent a row of the text. Is that right?

My problem is that, i do not know the length of the text, so how can i store it inside a dynamical array? I cannot use malloc :/

Here is where a C++ like string in C and a C++ getline function in C can be so handy ...
Just an op for readLine.h or readWord.h to the rescue to read lines and words of any length limited only by available memory.
Also CvecOfString.h or CListOfString.h are 'ready to roll' ...

http://developers-heaven.net/forum/index.php/topic,2580.msg2864.html#msg2864


Or ...
http://www.dreaminco...5&#entry1436894

This post has been edited by David W: 06 January 2012 - 02:53 PM

Was This Post Helpful? 0
  • +
  • -

#7 theonegandalf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 20-November 11

Re: Question about an exercise in C

Posted 07 January 2012 - 09:45 AM

Here is what i've done so far

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

void checkword( char *text, char *word );

int main( void )
{
	char *text, *word;

	// Text input
	text = ( char * )malloc( 500 *sizeof( char ) );
	word = ( char * )malloc(  100 * sizeof( char ) );
	fflush( stdin );
	gets( text );
	printf( "Give me the word you wanna search for : " );
	fflush( stdin );
	gets( word );
	checkword( text, word );
}

void checkword( char *text, char *word )
{
	int wordsize;
	int i = 0; // Μετρητής για τον πίνακα text
	int j = 0; // Μετρητής για τον πίνακα word
	int k = 0; // Μετρητής for
	int plithos = 0;

	wordsize = strlen( word ); // Word length

	while( text[ i ] != '\0' )
	{
		if ( word[ 0 ] == text[ i ] )
		{
			plithos = 1;

			for ( k = i + 1;  k < i + wordsize; k++ )
			{
				if ( word[ plithos ] == text[ k ] )
				{
					plithos++;
				}
			}

			if ( plithos == wordsize )
			{
				if ( i == 0 )
				{
					if ( isspace( text[ i + wordsize ] ) )
					{
						printf( "I Found your word\n" );
					}
				}
				else
				{
					if ( isspace( text[ i - 1 ] ) && ( isspace( text[ i + wordsize ] ) || text[ i + wordsize ] != '\0' ) )
					{
						printf( "I Found your word\n" );
					}
				}
			}
		}
		i++;
	}
}



I'm still far from the solution, that's for sure. In this part of the code i've only focused to try to implement the first question. But still no luck. It isn't working as it should. I've readed all your answers, and thank you all.

@JackOfAllTrades
I've readed your code but somewhere in there i got lost. I'm still newbie in C :/
The biggest problem is that i ain't got much time cause on Wednesday i have to give the exercises and i haven't finished them all. Still got 3 to do including this. Anyway as i said earlier i am a newbie in C, so what i wanna ask from you is if someone
can explain to me as clearly as he can how can i read a string from the keyboard of unknown length. The easiest way so i can understand it :) Also it would be great if he could explain this to me with an example( which includes code ).

Thank you all again! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1