strchr函数的用法
生活随笔
收集整理的這篇文章主要介紹了
strchr函数的用法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
【FROM MSDN && 百科】
原型:?char *strchr(const char *s,char c);
#include<string.h>
查找字符串s中首次出現字符c的位置,返回首次出現c的位置的指針,如果s中不存在c則返回NULL。
//#define FIRST_DEMO #define SECOND_DEMO #ifdef FIRST_DEMO #include <stdio.h> #include <conio.h> #include <string.h> #pragma warning (disable:4996) int main(void) {char string[17];char *ptr;char c='T';strcpy(string,"This is a string");ptr=strchr(string,c);if (ptr){printf("The character %c is at position:%d\n",c,ptr-string);}else{printf("The character was not found\n");}getch();return 0; } #elif defined SECOND_DEMO #include <stdio.h> #include <conio.h> #include <string.h> #pragma warning (disable:4996) int main(void) {char answer[100];char *p;printf("Type something:\n");fgets(answer,sizeof answer,stdin);if ((p=strchr(answer,'\n'))!=NULL) /*fgets不會像gets那樣自動地去掉結尾的\n,所以程序中手動將\n位置處的值變為\0,代表輸入的結束。*/{*p='\0';}printf("You typed \"%s\"\n",answer);getch();return 0; } #endif
總結
以上是生活随笔為你收集整理的strchr函数的用法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: TLS线程局部存储--thread_sp
- 下一篇: 判断子字符串是否出现在主字符串内