HELP!!! (solving task)

Reply

Join Date: Oct 2006
Posts: 2
Reputation: gwinera is an unknown quantity at this point 
Solved Threads: 0
gwinera gwinera is offline Offline
Newbie Poster

HELP!!! (solving task)

 
0
  #1
Oct 28th, 2006
Hello!

I am first year student and i really need your help with writing very simple assignment! I am sure that this will be easy to solve for you!

I have done something but it is not working! Can you check why?:

Division of text into sentences.
Write filter that divides the input text into sentences.
Remarks:
  • 􀂃 Each sentence sends with one of the following characters: '.", '?', or '!'.
  • 􀂃 Use a special function isSentEnd to detect characters ending a sentence.
  • 􀂃 The isSentEnd function has the following prototype: bool isSentEnd(int c);
  • 􀂃 The ending character must be included in the sentence. The new line character '\n' follows immediately.
  • 􀂃 Spaces, tabulation and new line characters separating sentences must be omitted.
  • 􀂃 The maximum allowed length of a sentence is 200 characters. Sentences exceeding the limit must be cut.
  • 􀂃 The sequences of white space characters (new line '\n' and tabulation '\t' and a regular space ' ' ) inside a sentence should be replaced by a single space.
And here is my filter:
  1. #include <vcl.h>
  2. #pragma hdrstop
  3. #include <stdio.h>
  4. #include <ctype.h>
  5. #include <conio.h>
  6. #include <string.h>
  7.  
  8. #pragma argsused
  9. bool isSentEnd(char wrd[], int size)
  10. {
  11. int c, i;
  12.  
  13. if (c==EOF) return(false);
  14. wrd[0]=c; i=1;
  15. while ((c=getchar())!=EOF && (c='?') || (c='!') || (c='.'))
  16.  
  17. {
  18.  
  19. if (i<size) wrd[i]=c; i++;
  20.  
  21. }
  22.  
  23. if (wrd[i]=('!' || '?' || '.'));
  24. return(strlen(wrd));
  25.  
  26. }
  27.  
  28. int main(int argc, char* argv[])
  29.  
  30. {
  31.  
  32. int c;
  33. char w[200];
  34. while (isSentEnd(w, sizeof(w)))
  35. puts(w);
  36. return 0;
  37. }
We are not sure how to solve this problem!

HELPPP!!! PLEASE
Last edited by Salem; Oct 28th, 2006 at 6:23 pm. Reason: Added code tags - learn how to use them
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,673
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 261
Lerner Lerner is offline Offline
Posting Virtuoso

Re: HELP!!! (solving task)

 
0
  #2
Oct 28th, 2006
First, I found a number of errors in the code as written:

1) Instructions say isSentEnd() is supposed to be sent a single int as a parameter, not a char array and an int.

2)
int c, i;
if (c==EOF) return(false);

c is not initialized before the == operator is called on it. This is an error.

3)
int c, i;
if (c==EOF) return(false);
wrd[0]=c;

Not surprisingly, c is still not initialized by the time you get to the third line above. Again an error.

4) (c='?') || (c='!') || (c='.')

These are all assignments to c. I suspect you meant checking for c equality with each of the char values instead.

5) wrd[i]=('!' || '?' || '.')

Again I suspect you wanted to check for equality, but you used the assignment operator instead, and you can't do multiple equality checks like that, in any event, you need to do them one at a time.

There may be more, but I'll stop there.

Furthermore, I think you need to answer the question of where is the input text coming from? I suspect it is coming from a file, however.

Assuming the input text is coming from a file I would start with the following logic to solve the problem:

Within main() I would declare a char array with 201 elements to hold the sentence as it is read from a file and I'd set the first char of the array to null. I'd then set up filepointer to read from the input file. I'd read the file one char at a time. If the current char read in is whitespace and the last char in sentence is not a space then I would add a space char to the holding array. If the char read in from the file isn't whitespace I would cast it to an int and send it to isSentEnd(). Within isSentEnd() I'd compare the int sent with the int values associated with the punctuation marks. If the int sent is a terminating punctuation mark or if the index of the array is 199 then the holding char array is null terminated and the resulting string is sent to another file, printed to the screen or whatever else you are told to do with it. I would then clear the holding string by assigning the null char to the first element of the holding char array and assign zero to index to get ready for the next sentence read in from the file. I would keep reading from the file until the char read in equalled EOF.

Here's how that logic may be written in pseudocode:
  1. #includes
  2.  
  3. bool isSentEnd(int sentInt)
  4. if sentInt equals int value of any of the terminating punctuation char
  5. return true
  6. else
  7. return false
  8.  
  9. int main()
  10.  
  11. char c = 'Z' //to hold char read from file
  12.  
  13. int i = 0; //to hold int value of char read in from file
  14.  
  15. int index = 0;
  16. char sentence with 201 elements //sentence can hold up to 200 char
  17. assign null char to element 0
  18.  
  19. FILE * p;
  20. open input file to read with p
  21. confirm file opened
  22.  
  23. while ((c = fgetc(p)) != EOF)
  24. if c is whitespace
  25. if sentence[index - 1] not equal space char
  26. assign space char to sentence[index];
  27. increment index;
  28. else
  29. assign c to sentence[index];
  30. increment index;
  31. cast c to an int and assign it to i;
  32. if(isSentEnd(i) OR index equals 200)
  33. assign null char to sentence[index]
  34. do what needs to be done with sentence
  35. assign null char to sentence[0]
  36. assign zero to index

I suspect there are errors in that logic as well, so use it only as an example of how you might go about answering the problem. You may need to do bounds checks associated with index to handle leading whitespace in addition to whitespace within the sentence, etc.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: HELP!!! (solving task)

 
0
  #3
Oct 29th, 2006
yes, are you expecting input from a text file or from the command prompt? Rather important to know.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2
Reputation: gwinera is an unknown quantity at this point 
Solved Threads: 0
gwinera gwinera is offline Offline
Newbie Poster

Re: HELP!!! (solving task)

 
0
  #4
Oct 29th, 2006
Originally Posted by iamthwee View Post
yes, are you expecting input from a text file or from the command prompt? Rather important to know.
YES I am expecting input from a text file.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC