splitting up a string (in C)

Started by
6 comments, last by RomanH 20 years, 3 months ago
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!
Advertisement
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;<br> }<br>u comapare the theStr ascii value to see which character is in it then u use the appropiate picture and blit it &#111;n screen<br>hope u got ur answer<br><br>_reza_ </i>
lost aixs
sorry for the prev post .. the above code is right........

char *theStr ="madCode";
for(int i=0;i{
cout<<theStr;
}
lost aixs
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
lost aixs
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]
"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
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)
"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
thankz for the info
lost aixs
Thanks for all the feedback! (I''m really starting to love this forum)

This topic is closed to new replies.

Advertisement