![]() |
[QUOTE=emily;290339]It's also worth noting that there is the D programming language which might be of interest if you want something similar to C++ but with some features of C# and Java. [url]http://en.wikipedia.org/wiki/D_(programming_language[/url])[/QUOTE]
Please present the shortest version of "Hello World" in D. |
The shortest non-obfuscated and easily readable Hello World in D:
import std.stdio; void main { writeln("Hello world"); } Now present me the shortest Hello World in Befunge. |
"Hello, World!\n" in [url=http://www.en.wikipedia.org/wiki/brainfuck]brainfuck[/url]:
[code] ++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.[/code] and [URL="http://en.wikipedia.org/wiki/Malbolge"]Malbolge[/URL]: [code]('&%:9]!~}|z2Vxwv-,POqponl$Hjig%eB@@>}=<M:9wv6WsU2T|nm-,jcL(I&%$#"`CB]V?Tx<uVtT`Rpo3NlF.Jh++FdbCBA@?]!~|4XzyTT43Qsqq(Lnmkj"Fhg${z@>[/code] |
[QUOTE=emily;290375]The shortest non-obfuscated and easily readable Hello World in D:
import std.stdio; void main { writeln("Hello world"); } Now present me the shortest Hello World in Befunge.[/QUOTE] run "emily\helloworld.d.exe" |
You know, the obfuscated C only made me want to learn more (goto, anyone?), but the whole I/O thing (and the not variable length arrays) is really annoying.
[url]http://publications.gbdirect.co.uk/c_book/chapter9/character_io.html[/url] In Java it's no problem to read in an entire String of any length and then create a char array of that length. Do you guys know anyway to count the chars on a line typed into stdin by the user? |
[QUOTE=Dubslow;290613]Do you guys know anyway to count the chars on a line typed into stdin by the user?[/QUOTE]
Given any char pointer *Str... CharCnt = strlen(Str); Keep in mind that this doesn't actually directly answer your question. That's because your question is not exact. C can handle a string (or any memory area) of any size that the operating system can. Much like Java, Perl et al... But since stdin can be a never ending stream, you have to take into consideration that just because you read a certain number of bytes at one time doesn't mean that's all there is. "Pipes" anyone? Also, I think you might be confusing STDIN with ARGV and ARGA. (Sorry... Slipped into Perl there... :smile:) |
[QUOTE=Dubslow;290613]Do you guys know anyway to count the chars on a line typed into stdin by the user?[/QUOTE]
Look up fgets() to get user input [it is safer to use than gets() or scanf() ], and then look up strlen() to find the length of a string. Just a quick google pulled up the following web page describing things to consider when getting user input: [url]http://www.daniweb.com/software-development/c/tutorials/45806[/url] I'm sure there are better ones out there, but this seemed a nice quick description of things that happen while getting input. |
See the problem (the first problem anyways) is that I don't want to limit the input to a certain number of characters; fgets takes an int n as the second argument, which is the length to read in. I have to allocate the char array of size n before reading in input, which means that
1) Input is limited to length n 2) If input is less than n, than I have a significant waste of memory The second prevents me from making n more than a few hundred. (It certainly doesn't affect the dinky little thing I'm writing, but a solution to this problem is good to know in general.) In Java, I can just get a line of input, take input.length() and that's the memory I need. That's why (@chalsall) I asked about the size of the stdin line, before C reads in the actual data with getchar() or fgets(). The second problem: (I used a manual read in because I'm not entirely confident in my ability to use fgets correctly, but this is equivalent as far as I can figure.) [code] printf("Please enter the source text (empty line to quit)\n"); char * string = (char *)malloc( MAXSTRLEN * sizeof(char)); for( i=0; ((c=getchar()) != EOF) && (i<(MAXSTRLEN-1)); i++) { if( c == '\n' ) { string[i]=0; break; } else string[i] = c; } if( i == (MAXSTRLEN - 1) ) { // Then the string is longer than MAXSTRLEN - 1, and so the rest is lost. string[MAXSTRLEN -1] = 0; printf("Source is longer than %d characters. The rest is lost.\n", MAXSTRLEN-1); } if ( string[0]==0 ) { // The entire string is null, or rather, the only character was \n, so no input. printf("Bye.\n"); return 7; }[/code] This doesn't wait for me to enter input, it just blows right past me. [code]Please enter the source text (empty line to quit) Bye.[/code]Edit: Could it be the EOF? In other progs (which work) I have the getchar() is only compared against \n, not against EOF. |
[QUOTE=Dubslow;290620]1) Input is limited to length n
2) If input is less than n, than I have a significant waste of memory[/QUOTE] Welcome to the wonderful world of C!!! :wink: Contemplate this... Write a C program which can handle and process "tail -f /var/log/messages | YOUR_PROGRAM". (Read: where stdin never ends.) BTW, your use of "getchar()" (which is a macro for "getc(stdin)") is terribly inefficient from an OS perspective. And your code depends on MAXSTRLEN being at least one more than the longest line you'll encounter. Lastly, with these functions EOF doesn't necessarily mean that there might not be more to read in the future -- only that there wasn't when the function was called. |
[QUOTE=chalsall;290622]And your code depends on MAXSTRLEN being at least one more than the longest line you'll encounter.[/QUOTE]Well that's rather the problem, isn't it?
[QUOTE=chalsall;290622] Lastly, with these functions EOF doesn't necessarily mean that there might not be more to read in the future -- only that there wasn't when the function was called.[/QUOTE] Okay, that makes sense. I'll drop the EOF then. Would getc(stdin) be more efficient? |
[QUOTE=Dubslow;290623]Would getc(stdin) be more efficient?[/QUOTE]
No. As I said, it's simply a macro -- the execution speed will be [B][I][U]exactly[/U][/I][/B] the same. As WraithX points out above, you want to get (no joke intended) friendly with fgets(). You work with what fgets() returns (if anything), and map it into your own data structures (possibily with dynamically allocated storage space) to parce as a function of what it has already been returned in the past. |
| All times are UTC. The time now is 21:46. |
Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2021, Jelsoft Enterprises Ltd.