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:

#include <vcl.h>
#pragma hdrstop
#include <stdio.h>
#include <ctype.h>
#include <conio.h>
#include <string.h>

#pragma argsused
bool isSentEnd(char wrd[], int size)
{
         int c, i;

         if (c==EOF) return(false);
         wrd[0]=c; i=1;
         while ((c=getchar())!=EOF && (c='?') || (c='!') || (c='.'))

         {

          if (i<size) wrd[i]=c; i++;

         }

         if (wrd[i]=('!' || '?' || '.'));
         return(strlen(wrd));

         }

         int main(int argc, char* argv[])

         {

         int c;
         char w[200];
         while (isSentEnd(w, sizeof(w)))
           puts(w);
          return 0;
          }

We are not sure how to solve this problem!

HELPPP!!! PLEASE

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=('!' || '?' || '.')

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:

#includes

bool isSentEnd(int sentInt)
   if sentInt equals int value of any of the terminating punctuation char
      return true
   else 
      return false

int main()

char c = 'Z'  //to hold char read from file

int i = 0; //to hold int value of char read in from file

int index = 0;
char sentence with 201 elements  //sentence can hold up to 200 char
assign null char to element 0

FILE * p;
open input file to read with p 
confirm file opened

while ((c = fgetc(p)) != EOF)
    if c is whitespace
      if sentence[index - 1] not equal space char
        assign space char to sentence[index];
        increment index;
    else
      assign c to sentence[index];
      increment index;
      cast c to an int and assign it to i;
      if(isSentEnd(i) OR index equals 200)
        assign null char to sentence[index]
        do what needs to be done with sentence
        assign null char to sentence[0]
        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.

Member Avatar for iamthwee

yes, are you expecting input from a text file or from the command prompt? Rather important to know.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.