//----------------------------------------------------
//AUTHOR: lanyang123456
//DATE: 2014-11-05
//---------------------------------------------------/*
str1.c
strstr(haystact, needl)
返回needl第一次在haystack出现的位置;
如果没找到,返回NULL;
test_macro???
*/
#include <string.h>
#include <stdio.h>
int main()
{
const char srcstr[] = "com from china city beijing";
const char str1[] = "china";
const char str2[] = "ok";
const char *retPointer = NULL;
retPointer = strstr(srcstr, str1);
if (retPointer == NULL)
{
printf("Not find the string %s.\n", str1);
}
else
{
printf("We find it and is: %s\n", retPointer);
}
retPointer = strstr(srcstr, str2);
if (retPointer == NULL)
{
printf("Not find the string %s.\n", str2);
}
else
{
printf("We find it and is: %s\n", retPointer);
}
return 0;
}
$ gcc -o str1 str1.c
$ ./str1
We find it and is: china city beijing
Not find the string ok.
/*
str3.c
char *strchr(const char *s, int c);
返回c在s中第一次出现的位置;若未找到,返回NULL;
char *strrchr(const char *s, int c);
返回c在s中最后一次出现的位置;若未找到,返回NULL;
*/
#include <string.h>
#include <stdio.h>
int main()
{
const char srcstr[] = "com from china city beijing";
const char chr1 = 'c';
const char chr2 = 'i';
const char *retPointer = NULL;
retPointer = strchr(srcstr, chr1);
if (retPointer == NULL)
{
printf("Not find the char %c.\n", chr1);
}
else
{
printf("We find it and is: %s\n", retPointer);
}
retPointer = strrchr(srcstr, chr2);
if (retPointer == NULL)
{
printf("Not find the char %c.\n", chr2);
}
else
{
printf("We find it and is: %s\n", retPointer);
}
return 0;
}
$ gcc -o str3 str3.c
$ ./str3
We find it and is: com from china city beijing
We find it and is: ing
参考man page