CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Find string

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
ROBOTICAR



Joined: 28 Aug 2007
Posts: 45

View user's profile Send private message

Find string
PostPosted: Mon Jul 14, 2008 11:45 am     Reply with quote

Is there any function for find one string in another string ? for example I want understand the place of "123" in "8469712365478".
Steve H
Guest







PostPosted: Mon Jul 14, 2008 12:00 pm     Reply with quote

This is the strstr() function - page 258 (in my manual).

Steve H.
ROBOTICAR



Joined: 28 Aug 2007
Posts: 45

View user's profile Send private message

PostPosted: Mon Jul 14, 2008 12:40 pm     Reply with quote

whats the type of result 'STRSTR()' ?
why If I want decimal result I must decrease '48' from result of STRSTR()?
Franck26



Joined: 29 Dec 2007
Posts: 122
Location: Ireland

View user's profile Send private message

PostPosted: Mon Jul 14, 2008 1:49 pm     Reply with quote

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.
ROBOTICAR



Joined: 28 Aug 2007
Posts: 45

View user's profile Send private message

PostPosted: Mon Jul 14, 2008 10:32 pm     Reply with quote

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' Shocked !!!!!!!!
Where is the problem? (my micro is (pic16F877A))
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Tue Jul 15, 2008 2:06 am     Reply with quote

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);
ROBOTICAR



Joined: 28 Aug 2007
Posts: 45

View user's profile Send private message

PostPosted: Tue Jul 15, 2008 12:39 pm     Reply with quote

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?
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Tue Jul 15, 2008 3:30 pm     Reply with quote

There is a bug in your program.
Code:
   unsigned int i;
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
Code:
   char *i;
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.
Code:
i=strstr(k1,k);
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:
Quote:
Ptr=1
123456
ROBOTICAR



Joined: 28 Aug 2007
Posts: 45

View user's profile Send private message

PostPosted: Tue Jul 15, 2008 10:52 pm     Reply with quote

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?
Guest








PostPosted: Wed Jul 16, 2008 2:00 am     Reply with quote

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'.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group