strchr函数的用法

【FROM MSDN && 百科】

原型: char *strchr(const char *s,char c);

#include

查找字符串s中首次出现字符c的位置,返回首次出现c的位置的指针,如果s中不存在c则返回NULL

//#define  FIRST_DEMO
#define  SECOND_DEMO
#ifdef FIRST_DEMO
#include 
#include 
#include 
#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 
#include 
#include 
#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


你可能感兴趣的:(-----c,c++心得-----)