
i want write code to split string to charachtar .
for example i have this string "Hello World"
i want get it like this
H
e
l
l
o
W
o
r
l
d
i need the idea please ::)
Code: Select all
char* MyMessage = "Hello World!":
// ^ Notice the * to make it a pointer
Code: Select all
PrintChar(MyMessage[4]);
Because 'o' is the 5th letter of the string (remember - arrays start at 0, not 1!)o
Code: Select all
void SplitString(char* Message){
int i = 0; //Counting variable
while(Message[i] != 0){ //While we haven't reached the end, marked by 0
PrintChar(Message[i]); //Print character from string
PrintChar('\n'); //Newline
i++;
}
}
Why do people always code so literally?Cjmovie wrote:Code: Select all
void SplitString(char* Message){ int i = 0; //Counting variable while(Message[i] != 0){ //While we haven't reached the end, marked by 0 PrintChar(Message[i]); //Print character from string PrintChar('\n'); //Newline i++; } }
Code: Select all
void SplitString( char * Message )
{
while ( *Message )
{
PrintChar( *Message++ );
PrintChar( '\n' );
}
}
Code: Select all
void SplitString( char * Message )
{
while ( PrintChar(*Message++) )
{
PrintChar( '\n' );
}
}
For understanding, solar. Elegance comes afterwards.Why do people always code so literally?
Because the code looks obvious. Your code got me thinking for 3 seconds before I figured it out, plus it's got an error which you didn't notice yet. It doesn't print newlines, but rather the entire string just as it was.Why do people always code so literally?
Code: Select all
void SplitString(char* Message){
int i = 0; //Counting variable
while(Message[i] != 0){ //While we haven't reached the end, marked by 0
PrintChar(Message[i]); //Print character from string
PrintChar('\n'); //Newline
i++;
}
}
Code: Select all
void SplitString(char* Message)
{
for (int i = 0; Message[i] != '\0'; i++)
{
PrintChar(Message[i]);
PrintChar('\n');
}
}
And why not? It's standard C lingo, and saves you one temporary variable on the stack...Joel (not logged in) wrote: Please! I beg you! No while(*Message++)ing!!![]()
Yeah, but it's much less readable (IMO). Clarity is goodSolar wrote: And why not? It's standard C lingo, and saves you one temporary variable on the stack...
That is what I meant with "standard C lingo": That kind of pointer incrementing is a very common procedure, especially in string operations where '\0' signifies the end of the sequence. I consider it to be more readable, at least if you're used to writing / reading C.troflip wrote: Yeah, but it's much less readable (IMO). Clarity is good![]()
Code: Select all
void test( char* dest, char const* src )
{
while ( *dest++ = *src++ );
}
Code: Select all
void test( char* dest, char const* src )
{
int i;
for ( i = 0; src[i] != '\0'; ++i )
{
dest[i] = src[i];
}
dest[i] = '\0';
}
I'd use C++, on the other hand, even if performance weren't critical, and C code sometimes falls into my hands. I'm not trying to say you should never write code like that in C. I am simply saying I think it makes code a little less readable. In many cases where I've seen it, it's premature optimization. In a string copy function, it can certainly be appropriate because it's probably something you're going to call often and you don't want to drain performance with an iniefficient implementation (although really the efficient version still should be avoided because it is prone to buffer overflows, so we need a loop counter anyway, plus an additional comparison).C is "portable high-level assembler", it's been designed as such, and the only reason I am using it is if the task at hand is performance-critical. (Actually I'm comfortable enough with C++ to use that for performance-critical stuff...) I haven't profiled the object code but I'd expect a performance benefit of roundabout 50%.
Well, that was my first real programming language (I knew a very little bit of Basic before that but not very much), so maybe it has influenced my styleI won't make this personal, but using a loop counter for iterating through a C string is so very Pascal...
I agree that strcpy() is insecure (and probably a bad example), but I challenge you to:Joel wrote: ...the efficient version still should be avoided because it is prone to buffer overflows, so we need a loop counter anyway, plus an additional comparison.
That is all I meant: For one really used to C, the pointer "magic" done in my first example holds no terrors, and is actually less error-prone than handling the loop counter, getting the conditional right and remembering to set the terminating '\0' manually.Well, [Pascal] was my first real programming language (I knew a very little bit of Basic before that but not very much), so maybe it has influenced my style![]()