| | |
removing white spaces
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Oct 2009
Posts: 28
Reputation:
Solved Threads: 0
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
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 ?
i have created this function so far
c Syntax (Toggle Plain Text)
void VymazBiele() { int c; while ( (c = getchar() ) != EOF ) { if (isspace(c)) { putchar(' '); while ( (c = getchar() ) != EOF && isspace(c)) {} } if (c != EOF) { putchar(c); } } }
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.
1
#2 Oct 7th, 2009
use a flag for that as follows
C Syntax (Toggle Plain Text)
#define FALSE 0 #define TRUE 1 void VymazBiele() { int c; int start= TRUE; while ( (c = getchar() ) != EOF ) { if (isspace(c)) { if(start==FALSE) putchar(' '); while ( (c = getchar() ) != EOF && isspace(c)) {} } if (c != EOF) { putchar(c); start = FALSE; } } }
•
•
Join Date: Oct 2009
Posts: 28
Reputation:
Solved Threads: 0
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
and the output is
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
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
C Syntax (Toggle Plain Text)
skuska skuska skuska test skuska test tessst k kjkj lkjkj
and the output is
C Syntax (Toggle Plain Text)
skuska skuska skuska test skuska test tessst k kjkj lkjkj
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.
•
•
Join Date: Oct 2009
Posts: 28
Reputation:
Solved Threads: 0
0
#6 Oct 8th, 2009
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 ?

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 ?
-1
#7 Oct 9th, 2009
•
•
•
•
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 ?
C Syntax (Toggle Plain Text)
#define FALSE 0 #define TRUE 1 void VymazBiele() { int c; int start= TRUE; int spacePrinted=FALSE; while ( (c = getchar() ) != EOF ) { if (isspace(c)) { spacePrinted = FALSE; while ( (c = getchar() ) != EOF && isspace(c)) {} } if (c != EOF) { if(start==FALSE && spacePrinted==FALSE) { putchar(' '); spacePrinted = TRUE; } putchar(c); start = FALSE; } } }
•
•
Join Date: Oct 2009
Posts: 28
Reputation:
Solved Threads: 0
0
#8 Oct 9th, 2009
There has to be some kind of mistake with this flag
input:
output
now it does not add the one space after the last letter, but it add space after the first letter..hmm ?
input:
C Syntax (Toggle Plain Text)
kjkkk kjkj jj
C Syntax (Toggle Plain Text)
k jkkk kjkj jj
Last edited by OSiRiSsk; Oct 9th, 2009 at 4:00 pm.
![]() |
Similar Threads
- 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)
- Removing white spaces from a string (C++)
- trying to remove extra white spaces in data file (IT Professionals' Lounge)
- file types: eliminating leading white spaces (C)
Other Threads in the C Forum
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays bash binarysearch centimeter char convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic fflush file fork frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hardware highest homework i/o ide inches infiniteloop initialization interest kilometer km lazy linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix meter microsoft motherboard multi mysql open opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling segmentationfault send shape single socketprograming socketprogramming spoonfeeding stack standard strchr string strings structures suggestions system systemcall test testautomation unix user voidmain() wab win32api windows.h





