Substring of an array in C - Page 1
Forum section: electronics / beginners

author: wicket
author_rank: Contributor
timestamp: March 28, 2014, 06:14:19 pm
content: Hi,I have two strings:Code:unsigned char string1[] = "ThisIsTheFirstString";unsigned char string2[] = "IsThe";Now I would like to extract 'IsThe' from string1 and compare it to string2 usingstrcmp()function. How can I do that? When using Arduino, there is asubstring(index1, index2)function, that does just that. Is there anything similar I can use when programming my PIC?

author: mariush
author_rank: Super Contributor
timestamp: March 28, 2014, 06:29:27 pm
content: Maybe use strncat to copy characters from first string into a new string, then use strcmp ?

author: Bored@Work
author_rank: Super Contributor
timestamp: March 28, 2014, 07:21:53 pm
content: Probably strstr() or strncmp() - it is not exactly clear what you want to do, and I rather drop dead then reading some Arduino documentation.

author: zapta
author_rank: Super Contributor
timestamp: March 28, 2014, 08:11:43 pm
content: You can do memcmp with size 5 (without the terminating null of string2).

author: IanB
author_rank: Super Contributor
timestamp: March 28, 2014, 09:11:25 pm
content: Quote from: wicket on March 28, 2014, 06:14:19 pmCode:int match;int offset = 4;size_t length = strlen(string2);match = strncmp(string1+offset, string2, length);offset is the index into string1 where the matching is to startlength is the number characters to match

author: wicket
author_rank: Contributor
timestamp: March 29, 2014, 11:37:09 am
content: Quote from: IanB on March 28, 2014, 09:11:25 pmOk, this works! Now I would like to go one step further - let's suppose I don't know the offset of "IsThe" in string1. How can I figure it out? I thinkstrstr(string1, string2)can be used, but I'm not quite sure how to deal with pointers.Quote

author: Kjelt
author_rank: Super Contributor
timestamp: March 29, 2014, 05:28:03 pm
content: use a for loop incrementing the offset from start to (with safemargin) ending.This is basic C, if you really want to do yourself a favour start with reading the C bible from Kernighan and Ritchie and start coding all the examples to get a feel of it.

author: IanB
author_rank: Super Contributor
timestamp: March 29, 2014, 07:10:10 pm
content: Quote from: wicket on March 29, 2014, 11:37:09 amYes, strstr might be used. But take not of what Kjelt says about C. You can't do anything non-trivial in C without working with pointers. (In fact, you probably can't do anything non-trivial on a micro like a PIC without working with pointers.) So you should get yourself a book on C like K&R and teach yourself how the language works.How to solve a problem depends on your high level objective. For instance "I want to find the location of a known sequence of characters in a larger string".If you ask the question this way, you can then state whether string2 is guaranteed to be correct, and whether or not string1 is guaranteed to contain string2 somewhere within it. Because there are two choices: string2 could exist in string1 at an unknown position you need to find, or string2 could be located at a known position in string1 but the spelling could be wrong. (If the position is unknown, AND there may be spelling errors, then the problem is effectively insoluble.)As for the specific question of strstr, you can find an offset as the difference between two pointers. For instance:Code:char *p;int offset;p = strstr(string1, string2);if (p) {offset = p - string1;} else {/* no match */}