943,968 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1736
  • C RSS
Oct 28th, 2006
0

HELP!!! (solving task)

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gwinera is offline Offline
2 posts
since Oct 2006
Oct 28th, 2006
0

Re: HELP!!! (solving task)

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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Oct 29th, 2006
0

Re: HELP!!! (solving task)

yes, are you expecting input from a text file or from the command prompt? Rather important to know.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Oct 29th, 2006
0

Re: HELP!!! (solving task)

Click to Expand / Collapse  Quote originally posted by iamthwee ...
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gwinera is offline Offline
2 posts
since Oct 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Can someone debug my program???
Next Thread in C Forum Timeline: Recording packets sent by external program





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC