Hello, is me again, im trying to improve my program with another function
it should print as many senteces on one line in the output, as user will enter as the parameter in the command line


sentenece is characterized with sign '.'
if you have sentences wchich ends with '...' then consider the last dot as an end of the sentence
but if you have '. . . ' (three dots with spaces after each one) then consider this as three different sentences

so i have this code, - i tried altered code that you suggested me in the previous topic
at first, ive just tried to do the basic thing, that it should count dots in the input
and if the number of dots equal the parameter then it should put new line

void ParameterSPL(int argc, char *argv[]) //spl means sentences per line
{
    unsigned long int counter = 0;
    unsigned long int pom; 
    int c;
    int start= TRUE;
    int spaceNeeded=TRUE;

    pom = SpracujParameter(argc, argv);
// in variable pom there is a number of sentences wich should be print on one 
// line in the output, in function SpracujParameter i used function
// strtoul, to convert parameter into the variable with data type unsigned lont int
                                                                    

      while ( (c = getchar() ) != EOF )
    {

        if (isspace(c))
        {
          spaceNeeded = FALSE;
          while ( (c = getchar() ) != EOF && isspace(c))
          {}
        }
        if (c == '.') // if there is an dot in the input
        {
          counter++; // then increase the variable counter
          }
          if (c != EOF)
          {
                if (start == FALSE && spaceNeeded == FALSE)
                {
                       putchar(' ');
                }
                       spaceNeeded = TRUE;
                putchar(c);
                if (counter == pom) // 
                {
                  putchar('\n'); // put new line
                  counter = 0; // reset the counter
                  }
                if(c == '\n')
                     start = TRUE;
                else
                     start = FALSE;
          }
    }
  }

so and the problem is here, i will demonstrate it on the input and outputs i have been testing

i entered number 3 as parameter

input

Tak teda vsetci vieme. Ze sa nemam dobre. Vsetko su sracky. Veronika na mna kasle.
Nechapem preco to robi. vy hej ?. ja teda nie. ved ma lubi. aspon tak vravi. hm. cele je to divne. chcem ju pri sebe.
a nie v pici daleko. preco za mnou nemoze dojst. preco za mnou nemoze dojst ked vravi ze ma lubi.

output

Tak teda vsetci vieme. Ze sa nemam dobre. Vsetko su sracky.
 Veronika na mna kasle. Nechapem preco to robi. vy hej ?.
 ja teda nie. ved ma lubi. aspon tak vravi.
 hm. cele je to divne. chcem ju pri sebe.
 a nie v pici daleko. preco za mnou nemoze dojst. preco za mnou nemoze dojst ked vravi ze ma lubi.

so as you can see, there is a white space in front of each new sentence, and there should not.... and how to code, that '...' will be recognized as one sentence ended with three dots, and '. . . ' will be recognized as three different sentences, i guess i can make this somehow with using macros, could i ? so could someone help me with this one ?

Just a few things to clear up.

1) How are existing new lines to be handled? If they should be ignored then line 35 should not output if c == EOL.

2) In the skip space loop you will skip new lines (isspace('\n') == true). So your test to restart skipping leading space is never true. You should fix that and restart skipping when your count is reached.

3) No need to check for EOF in the skip space loop ( isspace(EOF) == false). So you first check after, line 24, should be if(c != EOF) then check for the '.' .

Edited 7 Years Ago by SVR: n/a

The number of sentences printed on the one line has to equal the value of the parameter which has been entered
the humber of sentences printed on the last line can be lower as the value of parameter which has been entered
sentence is characterized with dot
but if sentence ends with ... then count it as only one sentence but if the sentence ends with . . . count it as three sentences, because sentence can be empty
so example
Test ... = one sentence
Test . . . = three sentences
so ive tried to change my code, to make this counting dots right, i used flag DOT for this, but it doesnt work like i want
here is my "new" code

void ParameterSPL(int argc, char *argv[])
{
    unsigned long int counter = 0;
    unsigned long int pom;
    int c;
    int start= TRUE;
    int spaceNeeded= FALSE;
    int dot = FALSE;

    pom = SpracujParameter(argc, argv);

      while ( (c = getchar() ) != EOF )
    {
        if (isspace(c))
        {
          spaceNeeded = TRUE;
          while ( (c = getchar() ) != EOF && isspace(c))
          {}
        }

          if (c != EOF)
          {
            if (c == '.')
                {
                  dot = TRUE;
                  }
                  if (dot == TRUE && spaceNeeded == TRUE)
                  {
                    counter++;
                    }
                if (start == FALSE && spaceNeeded == TRUE)
                {
                       putchar(' ');
                }
                  if ( counter == pom)
                  {
                    putchar('\n');
                    counter = 0;
                    dot = FALSE;
                   }
                  spaceNeeded = FALSE;
                putchar(c);

                if(c == '\n')
                     start = TRUE;
                else
                     start = FALSE;
          }
          }
                  }

parameter = 2,
input

Test... Test... Test...

output

Test... Test...
Test...

everything is fine so far

parameter = 2
input

Test... Test . . .

output

Test... Test
. .
.

and its wrong, output should be like this

Test... Test .
. .

i guess my condition on counting dots is not okay, and i still dont know where to put the last condtition
if(c == '\n')
start = TRUE;
else
start = FALSE;
...?

Edited 7 Years Ago by OSiRiSsk: n/a

The last condition should not check for new line any more. You want to restart skipping after the last sentence now (when the count is reached as I said). In other words when you do putchar('\n');

The condition for triple dots should reset after any non dot. You shouldn't check for spaceNeeded.

At line 35 you can see the reason the output was wrong ...

if ( counter == pom)
{
   putchar('\n');
   counter = 0;
   dot = FALSE;
}
spaceNeeded = FALSE;
putchar(c);

When the dot after Test is hit you output the newline THEN 'c', so the dot ends up on the next line.

Sorry for the double post, could not edit for some reason...

Another look, in order to catch the next non dot correctly you have to check before the skip space loop.

#define FALSE 0
#define TRUE 1

void ParameterSPL(int argc, char *argv[])
{
    unsigned long int counter = 0;
    unsigned long int pom;
 
    int c;
    int isTrailing  = FALSE;
    int spaceNeeded = FALSE;
    int lineNeeded  = FALSE;

    pom = SpracujParameter(argc, argv);

    while((c = getchar()) != EOF)
    {
        if(c != '.')
        {
            if(lineNeeded)
            {
                if(++counter == pom)
                {
                    putchar('\n');
                    isTrailing = FALSE;
                    counter = 0;
                }
               
                lineNeeded = FALSE;
            }

        }

        if (isspace(c) )
        {
            spaceNeeded = isTrailing;
            while ( (c = getchar() ) != EOF && isspace(c)) {}
        }

        if (c != EOF)
        {
            isTrailing = TRUE;

            if(spaceNeeded)
            {
                putchar(' ');
                spaceNeeded = FALSE;
            }

            putchar(c);

            if(c == '.')
            {
                lineNeeded = TRUE;
            }
        }

    }
}

Edited 7 Years Ago by SVR: n/a

Sorry for the double post, could not edit for some reason...

Another look, in order to catch the next non dot correctly you have to check before the skip space loop.

#define FALSE 0
#define TRUE 1

void ParameterSPL(int argc, char *argv[])
{
    unsigned long int counter = 0;
    unsigned long int pom;
 
    int c;
    int isTrailing  = FALSE;
    int spaceNeeded = FALSE;
    int lineNeeded  = FALSE;

    pom = SpracujParameter(argc, argv);

    while((c = getchar()) != EOF)
    {
        if(c != '.')
        {
            if(lineNeeded)
            {
                if(++counter == pom)
                {
                    putchar('\n');
                    isTrailing = FALSE;
                    counter = 0;
                }
               
                lineNeeded = FALSE;
            }

        }

        if (isspace(c) )
        {
            spaceNeeded = isTrailing;
            while ( (c = getchar() ) != EOF && isspace(c)) {}
        }

        if (c != EOF)
        {
            isTrailing = TRUE;

            if(spaceNeeded)
            {
                putchar(' ');
                spaceNeeded = FALSE;
            }

            putchar(c);

            if(c == '.')
            {
                lineNeeded = TRUE;
            }
        }

    }
}

this code works perfectly ! thx very much, but could you provide this code with commentz ? i would like to know what does exact line do exxactly, because i dont understand few things, THX VERY MUCH

this code works perfectly ! thx very much, but could you provide this code with commentz ? i would like to know what does exact line do exxactly, because i dont understand few things, THX VERY MUCH

This code is the same as you had. Just re-arranged a little.
I moved the sentence check first to catch the next non dot. The way it was (after the space skip) made it impossible to tell between the '. . .' and '...'

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        // look for first non dot after dot to count sentences & add new line

        if(c != '.')                         // if this is non dot ( to ignore '...')
        {
            if(lineNeeded)                   // there has been a dot so we need a line                   
            {
                if(++counter == pom)         // is there enough sentences?
                {
                    putchar('\n');           // yes - new line
                    isTrailing = FALSE;      // reset space skipping
                    counter = 0;             // reset sentence count
                }
               
                lineNeeded = FALSE;          // this sentence is handled
            }

        }

Next the space skipper

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        // skip spaces - reserve 1 space if trailing.

        if (isspace(c) )
        {
            spaceNeeded = isTrailing;        // set spaceNeeded to true if trailing
            while ( (c = getchar() ) != EOF && isspace(c)) {}
        }

Then handle non space characters

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        // check non space

        if (c != EOF)                        // are we done yet?
        {
            isTrailing = TRUE;               // we are now trailing (any leadin space
                                             // was skipped above)
            if(spaceNeeded)                  // were there trailing spaces? 
            {
                putchar(' ');                // if so ouput only one
                spaceNeeded = FALSE;         // trailing space handled
            }

            putchar(c);                      // output the non space char

            if(c == '.')                     // was it a dot?
            {
                lineNeeded = TRUE;           // yes, start line check
            }
        }

thx very much, now ive got it all, just the one last think

i want to make that if there is on input this

sentence1.sentence2

output should look like this

sentence1. sentence2

how to make this one ?

You should be able to see how by now.
What controls space output? The variable spaceNeeded. If true it will output a space before the next character.

Now where should it be set to true?

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        // look for first non dot after dot to count sentences & add new line

        if(c != '.')                         // if this is non dot ( to ignore '...')
        {
            if(lineNeeded)                   // there has been a dot so we need a line                   
            {
                if(++counter == pom)         // is there enough sentences?
                {
                    putchar('\n');           // yes - new line
                    isTrailing = FALSE;      // reset space skipping
                    counter = 0;             // reset sentence count
                }
                spaceNeeded = isTrailing;    // *** set spaceNeeded to true if trailing
                lineNeeded = FALSE;          // this sentence is handled
            }

        }
This question has already been answered. Start a new discussion instead.