removing white spaces

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

removing white spaces

 
0
  #1
Oct 7th, 2009
Hello, i have trouble with creating funkction that will remove white spaces if they are more than 1 in a row

i have created this function so far
  1. void VymazBiele()
  2. {
  3. int c;
  4. while ( (c = getchar() ) != EOF )
  5. {
  6. if (isspace(c))
  7. {
  8. putchar(' ');
  9. while ( (c = getchar() ) != EOF && isspace(c))
  10. {}
  11. }
  12. if (c != EOF)
  13. {
  14. putchar(c);
  15. }
  16. }
  17. }
but i also need remove white spaces at the stard of the line and at the end of the line and this function cant do this
i figured out that i can replace the first and last white spaces with something like this

if isspace(c)
{
putchar('\0')
}

but i dont know how to " ask " on the first char, i can use only standard function like getchar, putchar... and i need to create condtion that will look like this
" putchar('\0') untill you find the first alphabetic character and then use function created above "

can someone help ?
Last edited by OSiRiSsk; Oct 7th, 2009 at 7:40 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 357
Reputation: dkalita will become famous soon enough dkalita will become famous soon enough 
Solved Threads: 55
dkalita's Avatar
dkalita dkalita is offline Offline
Posting Whiz
 
1
  #2
Oct 7th, 2009
use a flag for that as follows
  1. #define FALSE 0
  2. #define TRUE 1
  3.  
  4. void VymazBiele()
  5. {
  6. int c;
  7. int start= TRUE;
  8. while ( (c = getchar() ) != EOF )
  9. {
  10. if (isspace(c))
  11. {
  12. if(start==FALSE)
  13. putchar(' ');
  14. while ( (c = getchar() ) != EOF && isspace(c))
  15. {}
  16. }
  17. if (c != EOF)
  18. {
  19. putchar(c);
  20. start = FALSE;
  21. }
  22. }
  23. }
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
  #3
Oct 7th, 2009
hups double post
Last edited by OSiRiSsk; Oct 7th, 2009 at 10:42 am.
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 7th, 2009
thanx you very much, but there is still one mistake
there CANT be any WHITE SPACE at the beginning of the line and at the END of the line

i used this test data sa input
  1. skuska skuska
  2.  
  3. skuska test skuska
  4.  
  5. test
  6.  
  7.  
  8.  
  9. tessst k
  10. kjkj
  11. lkjkj

and the output is
  1. skuska skuska skuska test skuska test tessst k kjkj lkjkj
you probably cant see it, but when u try to quote my post, you will see that there is one SPACE after the last word in the output and there can not be, so how to fix that ?

and plz, can u explain me, why using your code was more successful than mine ? what did that #define TRUE, #define FALSE do excactly /, and using this in the code, i relly want to understand it
Last edited by OSiRiSsk; Oct 7th, 2009 at 11:02 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 357
Reputation: dkalita will become famous soon enough dkalita will become famous soon enough 
Solved Threads: 55
dkalita's Avatar
dkalita dkalita is offline Offline
Posting Whiz
 
0
  #5
Oct 7th, 2009
Originally Posted by OSiRiSsk View Post
and plz, can u explain me, why using your code was more successful than mine ? what did that #define TRUE, #define FALSE do excactly /, and using this in the code, i relly want to understand it
read more on macros in C
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
  #6
Oct 8th, 2009
Originally Posted by dkalita View Post
read more on macros in C
ok, i understand now, that it will not do putchar(' ') because the START has true value, so it will ignore all the white spaces until the first letter

but how to manage, that the output won't end with one space as it ends in this case.. i cant figure out how to replace last white space with \0, any ideas ?
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 357
Reputation: dkalita will become famous soon enough dkalita will become famous soon enough 
Solved Threads: 55
dkalita's Avatar
dkalita dkalita is offline Offline
Posting Whiz
 
-1
  #7
Oct 9th, 2009
Originally Posted by OSiRiSsk View Post
ok, i understand now, that it will not do putchar(' ') because the START has true value, so it will ignore all the white spaces until the first letter

but how to manage, that the output won't end with one space as it ends in this case.. i cant figure out how to replace last white space with \0, any ideas ?
use one more flag for that as
  1. #define FALSE 0
  2. #define TRUE 1
  3.  
  4. void VymazBiele()
  5. {
  6. int c;
  7. int start= TRUE;
  8. int spacePrinted=FALSE;
  9. while ( (c = getchar() ) != EOF )
  10. {
  11. if (isspace(c))
  12. {
  13. spacePrinted = FALSE;
  14. while ( (c = getchar() ) != EOF && isspace(c))
  15. {}
  16. }
  17. if (c != EOF)
  18. {
  19. if(start==FALSE && spacePrinted==FALSE)
  20. {
  21. putchar(' ');
  22. spacePrinted = TRUE;
  23. }
  24. putchar(c);
  25. start = FALSE;
  26. }
  27. }
  28. }
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
  #8
Oct 9th, 2009
There has to be some kind of mistake with this flag

input:
  1. kjkkk kjkj jj
output
  1. k jkkk kjkj jj
now it does not add the one space after the last letter, but it add space after the first letter..hmm ?
Last edited by OSiRiSsk; Oct 9th, 2009 at 4:00 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
  #9
Oct 9th, 2009
The code as is should work right the first time. If you are reading multiple lines it will start putting leading spaces in because start is not reset for the next 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
 
0
  #10
Oct 11th, 2009
Originally Posted by SVR View Post
The code as is should work right the first time. If you are reading multiple lines it will start putting leading spaces in because start is not reset for the next one.
well but i will read multiple lines, so how to make this code right ?
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



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC