strchr strstr

本文介绍了C语言中strchr和strstr两个字符串处理函数的基本用法及实例演示。strchr用于查找字符首次出现的位置,而strstr则用于在字符串中搜索子串首次出现的位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*
函数 strchr()
功 能: 在一个串中查找给定字符的第一个匹配之处/
用 法: char *strchr(char *str, char c);
程序例:
The character r is at position: 12   n
*/
#include  <stdio.h>
#include <string.h>

int main(void)
{
char string[15];
char *ptr, c = 'r';

strcpy(string, "This is a string");
ptr = strchr(string, c);
if (ptr)
//printf("1111") ;
printf("The character %c is at position: %d   %c/n", c, ptr-(char*)string,ptr[2]);
else
printf("The character was not found/n");
return 0;
}

 

/*
函数 strstr()

原型:extern char *strstr(char *haystack, char *needle);
用 法: #include <string.h>
功能:从字符串haystack中寻找needle第一次出现的位置(不比较结束符NULL)。
说明:返回指向第一次出现needle位置的指针,如果没找到则返回NULL。

程序例:
The character r is at position: 2   s
*/
#include  <stdio.h>
#include <string.h>

int main(void)
{

char string[15];
char *ptr, c = 'r';

strcpy(string, "This is a string");
ptr = strstr(string, "is");
if (ptr)
//printf("1111") ;
printf("The character %c is at position: %d   %c/n", c, ptr-(char*)string,ptr[8]);
else
printf("The character was not found/n");
return 0;

#if 0
        char *s="Golden Global View";
        char *l="lob";
        char *p;
       
//        clrscr();
       
        p=strstr(s,l);
        if(p)
          printf("%s",p);
        else
          printf("Not Found!");
#endif
        getchar();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值