Strings, Char, and Arrays

Jassper, just fyi:

This is the page and site where the standard libraries for the C/C++ Arduino uses is:
http://www.nongnu.org/avr-libc/user-manual/modules.html

These are the string.h functions with some explanation:
http://www.nongnu.org/avr-libc/user-manual/group__avr__string.html

And this is strchr( stringToFindTheCharIn, charValue ) which is what InStr() does:

The strcat() function returns a pointer to the resulting string dest.

char * strchr ( const char * src,
int val
)

Locate character in string.

The strchr() function returns a pointer to the first occurrence of the character val in the string src.

Here "character" means "byte" - these functions do not work with wide or multi-byte characters.

Returns:
The strchr() function returns a pointer to the matched character or NULL if the character is not found.

That said I applaud making the effort to "roll your own" as you do learn more that way.

What you are trying to do is called parsing. Keep moving the pointer up and work as if each new step is a test, print out values to prove to yourself what happens.

The usual use of C++ String objects is based on not knowing what's going on 'inside the box' and has led to more "Help! I have a Problem" threads than any other programming issue here. The reason is simple: the average Arduino does not have enough RAM to do much when C++ Strings are used.

Well, most persons probably also don't know what's going on 'inside the box' when they use the strcat() function. People will use tools that work for them, and newbies like me find the String functions useful (and so far I haven't had issues using Strings). As to your statement that use of String objects has led to the most "help" request is simply something you apparently have made up to bolster your argument. I've followed this issue fairly closely and "help" request where certain persons always say "don't use Strings" rarely ever had issues caused by the use of Strings. These same people could have just effective in actually fixing the issue by saying "say no to drugs".

BTW, haven't you learned the simplicity of C strings yet? You've been here for years....

I use the tools that get what I need done. C strings is not so simple, otherwise why is this thread 20+ post long and still no resolution?

GoForSmoke:
BTW, haven't you learned the simplicity of C strings yet? You've been here for years....

Yes I have been here for years, but I have not been programing for years. It's not something I do on a consistent basis. By trade I am an electrical engineer and only dabble on the software side. For some reason parsing data and handling incoming information has been a huge crutch for me. I'm not sure why but one day I'll get it.

I recently got this book here, http://www.amazon.com/Without-Fear-Beginners-Guide-Edition/dp/0132673266
Hopefully it's a good one.

Thanks for the links and suggestions.

Jassper:

GoForSmoke:
BTW, haven't you learned the simplicity of C strings yet? You've been here for years....

Yes I have been here for years, but I have not been programing for years.

Sorry, that comment, was for Zoomkat who has spent more time championing C++ Strings than it takes to learn C strings more than a couple times over.

It's not something I do on a consistent basis. By trade I am an electrical engineer and only dabble on the software side. For some reason parsing data and handling incoming information has been a huge crutch for me. I'm not sure why but one day I'll get it.

I recently got this book here, http://www.amazon.com/Without-Fear-Beginners-Guide-Edition/dp/0132673266
Hopefully it's a good one.

Thanks for the links and suggestions.

All the things you can do with text is not a small study. If you understand what strings are made of, arrays of char variables and learn to use pointers (address holders) then you can learn function names to look up as you need. Probably the hardest basic part is learning pointers but the payoff goes beyond strings.

As for the functions, keep a table or page with the names and syntax of the handful you mostly will use, far less than half, just for reference. There's no need to memorize it all, the names follow patterns (it helps to be familiar with those) and you can find what you want easily enough from the docs.

strcpy() --- string copy, the string copied can be text in double quotes
strcat() --- string conCATenate, add a string to the end of another
strchr() --- string character, find a character in a string
strcmp() -- string compare
strlen() --- string length
strstr() ---- string string, find a substring within a string

There are versions of these with the letter l in them that take a length argument.
There are versions of these with the letter n in them that do not add a null at the end, which for example lets you paste one string inside another.

less used:
strrev() --- string reverse
stpbrk() --- finds the first character from a list within a string; ex: stpbrk( textArray, " ,.;:" )
strtok() --- string tokenize, has dual behavior that lets you parse text in a loop
strsep() --- also for parsing and a bit simpler than strtok()

Every last one of these is only working with arrays of 8-bit ASCII values with a 0 (NULL) at the end.

The mem functions are also useful, they work on generic bytes and don't stop at NULLs.

I've only skimmed it over a bit, but already has been a huge help.

http://cslibrary.stanford.edu/101/EssentialC.pdf

Have you learned pointers? It's easier if you know pointers before tackling strings because strings use pointers so much.

Pointers don't need no stinking indexes. You move pointers by adding or subtracting to jump sizeof(variable type) x offset value bytes. Even ptr++ and ++ptr, etc, work. Char pointers move 1 byte, int pointers move 2 bytes, etc. So ptr++ always means "move me to the next value". Just make sure there is one, check limits or write the code some way where limits never get broken.

GoForSmoke:
Have you learned pointers? It's easier if you know pointers before tackling strings because strings use pointers so much.

I have a basic grasp of pointers yes. My biggest issues is knowing where the pointer currently pointing :fearful:

The name of every array is a pointer to the start.

untested:

char buffer[ 32 ];
char *buf = buffer; // buf points to the start of buffer
char alf = 'A';

while ( buf < buffer + 31 ) // leave the last to fill with NULL
{
  *buf = alf++;
  buf++;
}
*buf = 0;