gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > char

#103537 - Borgi - Fri Sep 22, 2006 6:41 pm

hey guys,
im a nub here, so sorry for that if this post is in the wrong place.

in palib, how can i do this-> while ( text != "" ) <- i would liketo do this, until text is not empty.. any suggestions?

#103542 - Edelnutte - Fri Sep 22, 2006 7:10 pm

Before calling your Function to input the text, you shoul initialize text[0] and fill in something unused so:
if(text[0]!="your_no_text_char)

or you could set a boolean variable to true as soon as any text has been put in and check the value in an if statement.

#103543 - gmiller - Fri Sep 22, 2006 7:19 pm

Here is a small snippet of code that will process a char from a string till the end of the string is encountered:

Code:

char MyString[] = "this is a test string to process";
char *text = MyString; // Initialize pointer to first char in the string

while (*text != '\0') { // Loop till a null char is found, standard delimiter for char
 // do something ....
 // ...
 // Since text is a pointer-to-char we need to do *text to get the char it points to
 text++; // Move pointer to next char
 }


#103547 - gmiller - Fri Sep 22, 2006 7:34 pm

The code does not remove the char from the string it just changes where the code is looking. To remove the char from the string is somewhat complex:

Code:

char string1[] = "This is our starting string";
char string2[40]; // just make sure this is longer of the same size as string1

while (string1[0] != '\0') {
 // do something
 // ...
 strcpy(string2, string1 + 1); // move data from 2nd char;
 strcpy(string1, string2);
 }



This creates unneeded data movement in most cases so the pointer method is better. If you are using C++ and the string class the assignment operator does much the same thing making sure to create a new storage location and copying the data there (deep copy).