Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C Programming
Removing white spaces and tab characters
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
Reply to thread
Message
[QUOTE="Keith Thompson, post: 2386200"] [i][i][i][i] Please accept the following as *constructive* criticism. main returns int, not void. Change "void main()" to "int main(void)". Calling printf() with no prototype in scope invokes undefined behavior; the best way to get a prototype is "#include <stdio.h>". The length of the source1 array, 50, is an arbitrary magic number. Change the value of source, and you get an overflow. You use tabs in a string literal. That's legal, but inadvisable; I can't tell by looking at the source whether those are tabs or blanks. Try "USA\tTops\tOlympics\ttally". The program does everything in main(). It would be far more useful to write a function that takes a string and returns (a pointer to) the string with the tabs replaced. (This raises questions about how to return a string from a function; there are several ways to do it.) In real life, you're probably going to want to process input from stdin or from a file. This would also make it much easier to test your program without recompiling it to change the input data; for example, you could have easily tried it with sequences of multiple tabs. You make no real use of tab_cnt; the program merely replaces each tab with a space. A sequence of two tabs is replaced with two spaces. At the point where you check whether tab_cnt>=1, that condition will always be true; tab_cnt is initialized to 0, is incremented just before the test, and is never set back to 0. I suspect you were trying to replace each sequence of one or more tabs with a single space; to do that, you'll need an index to track where you are in source and another to track where you are in source1. BTW, naming your variables "source" and "target" would have made for clearer code than "source" and "source1". The program's output is not terminated by a newline. It's implementation-defined whether the last line of output requires a newline. On some systems, your program might print nothing at all. You don't return a value from main() or call exit(). In C99, this is equivalent to "return 0;", which is ok (or it would be if you had declared main() correctly); in C90, it returns an undefined termination status to the host environment. Your indentation is questionable. You use tab characters for indentation; it's better to use spaces instead, especially when posting to Usenet. Assuming 8-column tab stops, many of us find 8-column indentation excessive, though that's not universal. The entire while loop is indented; for consistency, the while(...) should be directly under the "int tab_cnt=0;". I was going to comment that replacing tabs by single spaces is less useful than replacing tabs with multiple spaces, depending on the current position and possibly on tab-stop settings, as the Unix "expand" program does -- but looking upthread I see that what you tried to do is based on the original problem statement (which was yours). Note that what you originally asked about was replacing each sequence of one or more spaces and tabs with a single space; your program does nothing special with spaces in the input string. Just for the sake of stating the problem more concretely, a Perl solution would be: perl -pe 's/[ \t]+/ /g' Ignore this is you don't know Perl (and don't expect to derive a C solution from this; C doesn't directly support regular expressions). If you happen to have Perl on your system, you can use this to check the results of your own C program.[/i][/i][/i][/i] [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C Programming
Removing white spaces and tab characters
Top