Author Topic: Substring of an array in C  (Read 2756 times)

0 Members and 1 Guest are viewing this topic.

Offline wicket

  • Contributor
  • Posts: 35
Substring of an array in C
« on: March 28, 2014, 06:14:19 pm »
Hi,

I have two strings:

Code: [Select]
unsigned char string1[] = "ThisIsTheFirstString";
unsigned char string2[] = "IsThe";

Now I would like to extract 'IsThe' from string1 and compare it to string2 using strcmp() function. How can I do that? When using Arduino, there is a substring(index1, index2) function, that does just that. Is there anything similar I can use when programming my PIC?
 

Online mariush

  • Super Contributor
  • ***
  • Posts: 3908
  • Country: ro
  • .
Re: Substring of an array in C
« Reply #1 on: March 28, 2014, 06:29:27 pm »
Maybe use strncat to copy characters from first string into a new string, then use strcmp ?
 

Offline Bored@Work

  • Super Contributor
  • ***
  • Posts: 3932
  • Country: 00
Re: Substring of an array in C
« Reply #2 on: March 28, 2014, 07:21:53 pm »
Probably strstr() or strncmp() - it is not exactly clear what you want to do, and I rather drop dead then reading some Arduino documentation.
I delete PMs unread. If you have something to say, say it in public.
For all else: Profile->[Modify Profile]Buddies/Ignore List->Edit Ignore List
 

Offline zapta

  • Super Contributor
  • ***
  • Posts: 6004
  • Country: us
Re: Substring of an array in C
« Reply #3 on: March 28, 2014, 08:11:43 pm »
You can do memcmp with size 5 (without the terminating null of string2).
Drain the swamp.
 

Offline IanB

  • Super Contributor
  • ***
  • Posts: 9618
  • Country: us
Re: Substring of an array in C
« Reply #4 on: March 28, 2014, 09:11:25 pm »
Hi,

I have two strings:

Code: [Select]
unsigned char string1[] = "ThisIsTheFirstString";
unsigned char string2[] = "IsThe";

Now I would like to extract 'IsThe' from string1 and compare it to string2 using strcmp() function. How can I do that? When using Arduino, there is a substring(index1, index2) function, that does just that. Is there anything similar I can use when programming my PIC?

Code: [Select]
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 start
length is the number characters to match
« Last Edit: March 28, 2014, 09:13:12 pm by IanB »
I'm not an EE--what am I doing here?
 

Offline wicket

  • Contributor
  • Posts: 35
Re: Substring of an array in C
« Reply #5 on: March 29, 2014, 11:37:09 am »
Code: [Select]
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 start
length is the number characters to match

Ok, 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 think strstr(string1, string2) can be used, but I'm not quite sure how to deal with pointers.

Quote
Function strstr(string1, string2) locates the first occurrence of the string2 in the string1 (excluding the terminating null character).

The function returns pointer to first occurrence of string2 in string1; if no string was found, function returns 0.
 

Online Kjelt

  • Super Contributor
  • ***
  • Posts: 5760
  • Country: nl
Re: Substring of an array in C
« Reply #6 on: March 29, 2014, 05:28:03 pm »
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.
 

Offline IanB

  • Super Contributor
  • ***
  • Posts: 9618
  • Country: us
Re: Substring of an array in C
« Reply #7 on: March 29, 2014, 07:10:10 pm »
Ok, 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 think strstr(string1, string2) can be used, but I'm not quite sure how to deal with pointers.

Quote
Function strstr(string1, string2) locates the first occurrence of the string2 in the string1 (excluding the terminating null character).

The function returns pointer to first occurrence of string2 in string1; if no string was found, function returns 0.

Yes, 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: [Select]
char *p;
int offset;

p = strstr(string1, string2);

if (p) {
    offset = p - string1;
} else {
    /* no match */
}
I'm not an EE--what am I doing here?
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf