| | |
Remowing white spaces + separating sentences
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Oct 2009
Posts: 28
Reputation:
Solved Threads: 0
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
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
output
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 ?
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
c Syntax (Toggle Plain Text)
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
C Syntax (Toggle Plain Text)
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.
C Syntax (Toggle Plain Text)
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.
•
•
Join Date: May 2008
Posts: 33
Reputation:
Solved Threads: 4
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 '.' .
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.
•
•
Join Date: Oct 2009
Posts: 28
Reputation:
Solved Threads: 0
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
parameter = 2,
input
output
everything is fine so far
parameter = 2
input
output
and its wrong, output should be like this
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;
...?
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
c Syntax (Toggle Plain Text)
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
C Syntax (Toggle Plain Text)
Test... Test... Test...
C Syntax (Toggle Plain Text)
Test... Test... Test...
parameter = 2
input
C Syntax (Toggle Plain Text)
Test... Test . . .
C Syntax (Toggle Plain Text)
Test... Test . . .
C Syntax (Toggle Plain Text)
Test... Test . . .
if(c == '\n')
start = TRUE;
else
start = FALSE;
...?
Last edited by OSiRiSsk; Oct 20th, 2009 at 4:57 pm.
•
•
Join Date: May 2008
Posts: 33
Reputation:
Solved Threads: 4
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 ...
When the dot after Test is hit you output the newline THEN 'c', so the dot ends up on the next line.
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 ...
C Syntax (Toggle Plain Text)
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.
•
•
Join Date: May 2008
Posts: 33
Reputation:
Solved Threads: 4
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.
Another look, in order to catch the next non dot correctly you have to check before the skip space loop.
C Syntax (Toggle Plain Text)
#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; } } } }
Last edited by SVR; Oct 21st, 2009 at 1:04 pm.
•
•
Join Date: Oct 2009
Posts: 28
Reputation:
Solved Threads: 0
0
#7 Oct 23rd, 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.
C Syntax (Toggle Plain Text)
#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; } } } }
•
•
Join Date: May 2008
Posts: 33
Reputation:
Solved Threads: 4
1
#8 Oct 23rd, 2009
•
•
•
•
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
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 '...'
C Syntax (Toggle Plain Text)
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // 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
C Syntax (Toggle Plain Text)
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // 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
C Syntax (Toggle Plain Text)
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // 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 } }
•
•
Join Date: Oct 2009
Posts: 28
Reputation:
Solved Threads: 0
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
output should look like this
how to make this one ?
i want to make that if there is on input this
C Syntax (Toggle Plain Text)
sentence1.sentence2
C Syntax (Toggle Plain Text)
sentence1. sentence2
how to make this one ?
•
•
Join Date: May 2008
Posts: 33
Reputation:
Solved Threads: 4
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?
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?
C Syntax (Toggle Plain Text)
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // 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 } }
![]() |
Similar Threads
- removing white spaces (C)
- string case & white spaces problem? (PHP)
- Reading white spaces... (C++)
- create blank charaters (white spaces) (C++)
- How to insert big html markup with white spaces(i.e. line breaks) and single and ... (MS SQL)
- Using isspace with white spaces (C++)
- trying to remove extra white spaces in data file (IT Professionals' Lounge)
- HELP!!! (solving task) (C)
- file types: eliminating leading white spaces (C)
Other Threads in the C Forum
- Previous Thread: Rot 13 Help
- Next Thread: How to split string into a structure?
| Thread Tools | Search this Thread |
adobe api array arrays binarysearch calculate char cm copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators initialization iso kernel kilometer km linked linkedlist linux linuxsegmentationfault list lists locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue multi mysql odf open opensource openwebfoundation owf pattern pdf performance pointer pointers posix power probleminc program programming pyramidusingturboccodes read recursion recv repetition research scanf scheduling scripting segmentationfault send shape socketprograming socketprogramming stack standard string strings suggestions systemcall test testautomation unix urboc user voidmain() wab win32api windows.h





