View previous topic :: View next topic |
Author |
Message |
ROBOTICAR
Joined: 28 Aug 2007 Posts: 45
|
Find string |
Posted: Mon Jul 14, 2008 11:45 am |
|
|
Is there any function for find one string in another string ? for example I want understand the place of "123" in "8469712365478". |
|
![](templates/subSilver/images/spacer.gif) |
Steve H Guest
|
|
Posted: Mon Jul 14, 2008 12:00 pm |
|
|
This is the strstr() function - page 258 (in my manual).
Steve H. |
|
![](templates/subSilver/images/spacer.gif) |
ROBOTICAR
Joined: 28 Aug 2007 Posts: 45
|
|
Posted: Mon Jul 14, 2008 12:40 pm |
|
|
whats the type of result 'STRSTR()' ?
why If I want decimal result I must decrease '48' from result of STRSTR()? |
|
![](templates/subSilver/images/spacer.gif) |
Franck26
Joined: 29 Dec 2007 Posts: 122 Location: Ireland
|
|
Posted: Mon Jul 14, 2008 1:49 pm |
|
|
I don't have the CCS manual with me, but usually we decrease 48 to convert an ASCII chart in a decimal representation:
ASCII -> Decimal
48 -> 0
49 -> 1
50 -> 2
etc...
Good Luck,
Franck. |
|
![](templates/subSilver/images/spacer.gif) |
ROBOTICAR
Joined: 28 Aug 2007 Posts: 45
|
|
Posted: Mon Jul 14, 2008 10:32 pm |
|
|
Thanks
and another problem. I always have awful problems with this compiler for example:
please attention to this code:
Code: |
#include "main.h"
#include "string.h"
void main()
{
char j[10];
char k[]="123",k1[]="000123456";
unsigned int i;
i=strstr(k1,k);
printf("I=%c",i);
} |
I don't know whats the relation between J and the result of the strstr().
Now this code give correct result it means Micro print :'3'
but when I change the capacity of J, the result change. It means when I define 'char j[20] ' the result change to '7' !!!!!!!!
Where is the problem? (my micro is (pic16F877A)) |
|
![](templates/subSilver/images/spacer.gif) |
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Tue Jul 15, 2008 2:06 am |
|
|
I is a pointer. printf("I=%c", i); displays the pointer value NOT it's contents.
The reason the value changes is because the arrays are stored in a slightly different memory location when you change the size of j. This is to allow for the extra space that j uses.
To print what is contained IN the pointer put an '*' at the front:-
printf("I=%c", *i); |
|
![](templates/subSilver/images/spacer.gif) |
ROBOTICAR
Joined: 28 Aug 2007 Posts: 45
|
|
Posted: Tue Jul 15, 2008 12:39 pm |
|
|
OK....I tried it. but.....
I change the code:
Code: |
#include "main.h"
#include "string.h"
void main()
{
char j[10];
char k[]="123",k1[]="000123456";
unsigned int i;
i=strstr(k1,k);
printf("I=%c",*i);
} |
but what's the result??!! the result is the first character of K[] it means '1'.
you can try....now where is the problem? |
|
![](templates/subSilver/images/spacer.gif) |
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Tue Jul 15, 2008 3:30 pm |
|
|
There is a bug in your program.You are using i as a pointer so it should be declared with a '*'. The difference is that an int is 1 byte in size and a pointer 2 bytes. The pointer points to a character and not to an integer so the improved code would look like Personally I would reserve the variable name 'i' only for simple integer loop variables. For other variables use a more meaningful name.
However, fixing the bug doesn't change the output as the '1' result of your previous test was correct. Here you are searching the string k1 ("000123456") for the first occurrence of the string k ("123"). What you get returned is the _address_ to the first character of the matching string in k1. Yes, this is the character '1', but that's what you were searching for, weren't you?
If the substring isn't found than strstr() would have returned 0 (null). Always test for this result.
Example: Code: | #include "main.h"
#include "string.h"
void main()
{
char k[]="123", k1[]="000123456";
char *ptr;
ptr=strstr(k1,k);
if (ptr==NULL)
printf("Not found");
else
{
printf("Ptr=%c", *ptr); // Prints the first character of the found string
printf("%s", ptr); // Prints the string starting with the first
// matching character
}
} | Will print:
|
|
![](templates/subSilver/images/spacer.gif) |
ROBOTICAR
Joined: 28 Aug 2007 Posts: 45
|
|
Posted: Tue Jul 15, 2008 10:52 pm |
|
|
Thanks .
The strstr() must return the number of place K in K1 in this programs means return '3' . but in this program that you wrote didn't return the number of place . so what's the way? |
|
![](templates/subSilver/images/spacer.gif) |
Guest
|
|
Posted: Wed Jul 16, 2008 2:00 am |
|
|
strstr returns a pointer to the address in memory where the found match starts, it doesn't return a position number!
If you want to find the position number you can do some address calculations: subtract the start address from the found address. Code: | // Find the position of substring y in a string x.
// Returns -1 when not found.
signed int8 strpos(char *x, char *y)
{
char *ptr;
signed int8 pos = -1;
ptr = strchr(x,y);
if (ptr != NULL)
{
pos = ptr - x; // pointer arithmetic!
}
return pos;
} | Note that this example function returns the position in the string starting at position 0, so you example would return '2' instead of '3'. |
|
![](templates/subSilver/images/spacer.gif) |
|