Archived

This topic is now archived and is closed to further replies.

RomanH

splitting up a string (in C)

Recommended Posts

RomanH    138
hello everybody, After years of playing around with VB I''m currently writing my frst game in C (it''s a 2d jump''n''run game and making surprisingly good progress). However I have a problem writing text on the screen. I''ve written a function to write a single character (well, it''s actually blitting a graphic) at the time. What I now need to know is how do I split a string into individual characters? (in C) Thanks!

Share this post


Link to post
Share on other sites
reza    122
here is a junk of turbo c code .. theStrwill contain each character of the theStr

char *theStr = "madCode";

for (int i=0;i {
cout<<theStr[i];
}
u comapare the theStr[i] ascii value to see which character is in it then u use the appropiate picture and blit it on screen
hope u got ur answer

_reza_

Share this post


Link to post
Share on other sites
reza    122
sorry for the prev post .. the above code is right........

char *theStr ="madCode";
for(int i=0;i{
cout<<theStr;
}

Share this post


Link to post
Share on other sites
reza    122
jeeeee man something is wrong with this posting thing ... it aint posting the code i am writing
the loop contains a check i = 0 ; i < strlen( theStr) ; i ++
plz adjust

Share this post


Link to post
Share on other sites
Fruny    1658
In C, strings really are just arrays of characters, with a '\0' to mark the end. So, you would do something like:


char mystring[] = "A string really is just an array of characters."

for(int index=0; mystring[index] != 0; ++index)
draw_character(mystring[index])

/* or, equivalently */

int index = 0;
while(mystring[index])
draw_character(mystring[index++]);


And, reza, your code has a number of problems. First, it is generally not recommended to use a char* to point at a string literal - better use a const char* or a char[]. Second, this cannot be Turbo C code, since C doesn't have operator overloading, and yet, you're trying to do C++ stream I/O


“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
— Brian W. Kernighan (C programming language co-inventor)


[edited by - Fruny on January 18, 2004 3:18:32 PM]

Share this post


Link to post
Share on other sites
Fruny    1658
quote:
Original post by reza
jeeeee man something is wrong with this posting thing ... it aint posting the code i am writing


It''s called HTML, < marks the beginning of a tag, and [‍i] is interpreted by the forum to mean italic. If you want to post code, better put it in a [‍source] box. That, or learn the HTML entity codes. For example, if you want to be sure you''ll get an <, you have to type &lt;. Simple, no ?


“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
— Brian W. Kernighan (C programming language co-inventor)

Share this post


Link to post
Share on other sites