Remowing white spaces + separating sentences

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2009
Posts: 28
Reputation: OSiRiSsk is an unknown quantity at this point 
Solved Threads: 0
OSiRiSsk OSiRiSsk is offline Offline
Light Poster

Remowing white spaces + separating sentences

 
0
  #1
Oct 18th, 2009
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
  1. void ParameterSPL(int argc, char *argv[]) //spl means sentences per line
  2. {
  3. unsigned long int counter = 0;
  4. unsigned long int pom;
  5. int c;
  6. int start= TRUE;
  7. int spaceNeeded=TRUE;
  8.  
  9. pom = SpracujParameter(argc, argv);
  10. // in variable pom there is a number of sentences wich should be print on one
  11. // line in the output, in function SpracujParameter i used function
  12. // strtoul, to convert parameter into the variable with data type unsigned lont int
  13.  
  14.  
  15. while ( (c = getchar() ) != EOF )
  16. {
  17.  
  18. if (isspace(c))
  19. {
  20. spaceNeeded = FALSE;
  21. while ( (c = getchar() ) != EOF && isspace(c))
  22. {}
  23. }
  24. if (c == '.') // if there is an dot in the input
  25. {
  26. counter++; // then increase the variable counter
  27. }
  28. if (c != EOF)
  29. {
  30. if (start == FALSE && spaceNeeded == FALSE)
  31. {
  32. putchar(' ');
  33. }
  34. spaceNeeded = TRUE;
  35. putchar(c);
  36. if (counter == pom) //
  37. {
  38. putchar('\n'); // put new line
  39. counter = 0; // reset the counter
  40. }
  41. if(c == '\n')
  42. start = TRUE;
  43. else
  44. start = FALSE;
  45. }
  46. }
  47. }

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
  1. Tak teda vsetci vieme. Ze sa nemam dobre. Vsetko su sracky. Veronika na mna kasle.
  2. Nechapem preco to robi. vy hej ?. ja teda nie. ved ma lubi. aspon tak vravi. hm. cele je to divne. chcem ju pri sebe.
  3. a nie v pici daleko. preco za mnou nemoze dojst. preco za mnou nemoze dojst ked vravi ze ma lubi.
output
  1. Tak teda vsetci vieme. Ze sa nemam dobre. Vsetko su sracky.
  2. Veronika na mna kasle. Nechapem preco to robi. vy hej ?.
  3. ja teda nie. ved ma lubi. aspon tak vravi.
  4. hm. cele je to divne. chcem ju pri sebe.
  5. 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 ?
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 28
Reputation: OSiRiSsk is an unknown quantity at this point 
Solved Threads: 0
OSiRiSsk OSiRiSsk is offline Offline
Light Poster
 
-2
  #2
Oct 20th, 2009
BUMP! anybody can help ?
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 33
Reputation: SVR is an unknown quantity at this point 
Solved Threads: 4
SVR SVR is offline Offline
Light Poster
 
0
  #3
Oct 20th, 2009
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 '.' .
Last edited by SVR; Oct 20th, 2009 at 4:19 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 28
Reputation: OSiRiSsk is an unknown quantity at this point 
Solved Threads: 0
OSiRiSsk OSiRiSsk is offline Offline
Light Poster
 
0
  #4
Oct 20th, 2009
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

  1. void ParameterSPL(int argc, char *argv[])
  2. {
  3. unsigned long int counter = 0;
  4. unsigned long int pom;
  5. int c;
  6. int start= TRUE;
  7. int spaceNeeded= FALSE;
  8. int dot = FALSE;
  9.  
  10. pom = SpracujParameter(argc, argv);
  11.  
  12. while ( (c = getchar() ) != EOF )
  13. {
  14. if (isspace(c))
  15. {
  16. spaceNeeded = TRUE;
  17. while ( (c = getchar() ) != EOF && isspace(c))
  18. {}
  19. }
  20.  
  21. if (c != EOF)
  22. {
  23. if (c == '.')
  24. {
  25. dot = TRUE;
  26. }
  27. if (dot == TRUE && spaceNeeded == TRUE)
  28. {
  29. counter++;
  30. }
  31. if (start == FALSE && spaceNeeded == TRUE)
  32. {
  33. putchar(' ');
  34. }
  35. if ( counter == pom)
  36. {
  37. putchar('\n');
  38. counter = 0;
  39. dot = FALSE;
  40. }
  41. spaceNeeded = FALSE;
  42. putchar(c);
  43.  
  44. if(c == '\n')
  45. start = TRUE;
  46. else
  47. start = FALSE;
  48. }
  49. }
  50. }

parameter = 2,
input
  1. Test... Test... Test...
output
  1. Test... Test...
  2. Test...
everything is fine so far

parameter = 2
input
  1. Test... Test . . .
output
  1. Test... Test
  2. . .
  3. .
and its wrong, output should be like this
  1. Test... Test .
  2. . .
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;
...?
Last edited by OSiRiSsk; Oct 20th, 2009 at 4:57 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 33
Reputation: SVR is an unknown quantity at this point 
Solved Threads: 4
SVR SVR is offline Offline
Light Poster
 
0
  #5
Oct 21st, 2009
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 ...
  1.  
  2. if ( counter == pom)
  3. {
  4. putchar('\n');
  5. counter = 0;
  6. dot = FALSE;
  7. }
  8. spaceNeeded = FALSE;
  9. putchar(c);

When the dot after Test is hit you output the newline THEN 'c', so the dot ends up on the next line.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 33
Reputation: SVR is an unknown quantity at this point 
Solved Threads: 4
SVR SVR is offline Offline
Light Poster
 
0
  #6
Oct 21st, 2009
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.

  1. #define FALSE 0
  2. #define TRUE 1
  3.  
  4. void ParameterSPL(int argc, char *argv[])
  5. {
  6. unsigned long int counter = 0;
  7. unsigned long int pom;
  8.  
  9. int c;
  10. int isTrailing = FALSE;
  11. int spaceNeeded = FALSE;
  12. int lineNeeded = FALSE;
  13.  
  14. pom = SpracujParameter(argc, argv);
  15.  
  16. while((c = getchar()) != EOF)
  17. {
  18. if(c != '.')
  19. {
  20. if(lineNeeded)
  21. {
  22. if(++counter == pom)
  23. {
  24. putchar('\n');
  25. isTrailing = FALSE;
  26. counter = 0;
  27. }
  28.  
  29. lineNeeded = FALSE;
  30. }
  31.  
  32. }
  33.  
  34. if (isspace(c) )
  35. {
  36. spaceNeeded = isTrailing;
  37. while ( (c = getchar() ) != EOF && isspace(c)) {}
  38. }
  39.  
  40. if (c != EOF)
  41. {
  42. isTrailing = TRUE;
  43.  
  44. if(spaceNeeded)
  45. {
  46. putchar(' ');
  47. spaceNeeded = FALSE;
  48. }
  49.  
  50. putchar(c);
  51.  
  52. if(c == '.')
  53. {
  54. lineNeeded = TRUE;
  55. }
  56. }
  57.  
  58. }
  59. }
Last edited by SVR; Oct 21st, 2009 at 1:04 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 28
Reputation: OSiRiSsk is an unknown quantity at this point 
Solved Threads: 0
OSiRiSsk OSiRiSsk is offline Offline
Light Poster
 
0
  #7
Oct 23rd, 2009
Originally Posted by SVR View Post
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.

  1. #define FALSE 0
  2. #define TRUE 1
  3.  
  4. void ParameterSPL(int argc, char *argv[])
  5. {
  6. unsigned long int counter = 0;
  7. unsigned long int pom;
  8.  
  9. int c;
  10. int isTrailing = FALSE;
  11. int spaceNeeded = FALSE;
  12. int lineNeeded = FALSE;
  13.  
  14. pom = SpracujParameter(argc, argv);
  15.  
  16. while((c = getchar()) != EOF)
  17. {
  18. if(c != '.')
  19. {
  20. if(lineNeeded)
  21. {
  22. if(++counter == pom)
  23. {
  24. putchar('\n');
  25. isTrailing = FALSE;
  26. counter = 0;
  27. }
  28.  
  29. lineNeeded = FALSE;
  30. }
  31.  
  32. }
  33.  
  34. if (isspace(c) )
  35. {
  36. spaceNeeded = isTrailing;
  37. while ( (c = getchar() ) != EOF && isspace(c)) {}
  38. }
  39.  
  40. if (c != EOF)
  41. {
  42. isTrailing = TRUE;
  43.  
  44. if(spaceNeeded)
  45. {
  46. putchar(' ');
  47. spaceNeeded = FALSE;
  48. }
  49.  
  50. putchar(c);
  51.  
  52. if(c == '.')
  53. {
  54. lineNeeded = TRUE;
  55. }
  56. }
  57.  
  58. }
  59. }
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
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 33
Reputation: SVR is an unknown quantity at this point 
Solved Threads: 4
SVR SVR is offline Offline
Light Poster
 
1
  #8
Oct 23rd, 2009
Originally Posted by OSiRiSsk View Post
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 '...'

  1. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2. // look for first non dot after dot to count sentences & add new line
  3.  
  4. if(c != '.') // if this is non dot ( to ignore '...')
  5. {
  6. if(lineNeeded) // there has been a dot so we need a line
  7. {
  8. if(++counter == pom) // is there enough sentences?
  9. {
  10. putchar('\n'); // yes - new line
  11. isTrailing = FALSE; // reset space skipping
  12. counter = 0; // reset sentence count
  13. }
  14.  
  15. lineNeeded = FALSE; // this sentence is handled
  16. }
  17.  
  18. }

Next the space skipper

  1. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2. // skip spaces - reserve 1 space if trailing.
  3.  
  4. if (isspace(c) )
  5. {
  6. spaceNeeded = isTrailing; // set spaceNeeded to true if trailing
  7. while ( (c = getchar() ) != EOF && isspace(c)) {}
  8. }

Then handle non space characters

  1. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2. // check non space
  3.  
  4. if (c != EOF) // are we done yet?
  5. {
  6. isTrailing = TRUE; // we are now trailing (any leadin space
  7. // was skipped above)
  8. if(spaceNeeded) // were there trailing spaces?
  9. {
  10. putchar(' '); // if so ouput only one
  11. spaceNeeded = FALSE; // trailing space handled
  12. }
  13.  
  14. putchar(c); // output the non space char
  15.  
  16. if(c == '.') // was it a dot?
  17. {
  18. lineNeeded = TRUE; // yes, start line check
  19. }
  20. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 28
Reputation: OSiRiSsk is an unknown quantity at this point 
Solved Threads: 0
OSiRiSsk OSiRiSsk is offline Offline
Light Poster
 
0
  #9
Oct 25th, 2009
thx very much, now ive got it all, just the one last think

i want to make that if there is on input this
  1. sentence1.sentence2
output should look like this
  1. sentence1. sentence2

how to make this one ?
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 33
Reputation: SVR is an unknown quantity at this point 
Solved Threads: 4
SVR SVR is offline Offline
Light Poster
 
0
  #10
Oct 29th, 2009
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?

  1. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2. // look for first non dot after dot to count sentences & add new line
  3.  
  4. if(c != '.') // if this is non dot ( to ignore '...')
  5. {
  6. if(lineNeeded) // there has been a dot so we need a line
  7. {
  8. if(++counter == pom) // is there enough sentences?
  9. {
  10. putchar('\n'); // yes - new line
  11. isTrailing = FALSE; // reset space skipping
  12. counter = 0; // reset sentence count
  13. }
  14. spaceNeeded = isTrailing; // *** set spaceNeeded to true if trailing
  15. lineNeeded = FALSE; // this sentence is handled
  16. }
  17.  
  18. }
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC