C primer plus 第六版 第十一章 第五题 编程练习答案

版权声明:转载请注明来源~ https://blog.csdn.net/Lth_1571138383/article/details/85232557

Github地址:φ(>ω<*)这里这里。

/*
    本次任务需设计并测试一个函数,
     该函数接受两个参数,通过第二个参数接受一个指定字符串,并在第一个参数中寻找其首次出现的位置。
      如果未找到则返回空指针。 使用一个循环给函数提供输入值。
    ps: 这个能不能偷懒直接用strchr()啊。。。。。。。。ヾ(o・ω・)ノ
*/

#include<stdio.h>

#define o 100

char * strfind(char * orig, char chr);

void get(char * get);

int main(void)
{	
	char orig[o] = {};
	char find = 0;
	char * place1 = NULL;
	char quit = 0;

	printf("Please input two arguments. \nThe first is a string and second is a character. \n"
			"The second argument is search in first argument,"
			" and return its exist place of first .\n\n");  // 英文不过小学水平不要介意。。大概意思知道就行。

	while( quit != 'q' )
	{
		printf("Now, Please input string:\n");
		get(orig);

		printf("\nNow, Please input character: ");
		scanf("%c", &find);
		fflush(stdin);

		place1 = strfind(orig, find);
		if( place1 != NULL )
		{
			printf("Find that character, do you want to quit or try again ?(Enter 'q' to quit) :");
			scanf("%c", &quit);
			fflush(stdin);
		}
		else
		{
			printf("We don't find it. do you want to quit or try again ?(Enter 'q' to quit) : ");
			scanf("%c", &quit);
			fflush(stdin);
		}
	}

	printf("\nBye ~\n");

	getchar();

	return 0;
}

char * strfind(char * orig, char chr)
{	
	char * place2 = NULL;

	while(*orig != '\n' && *orig != EOF)
	{
		place2 = (*orig == chr ? orig : NULL ); 

		if( place2 == NULL)
		{
			orig++;
		}
		else
		{
			break;
		}
	}

	return place2;
}

void get(char * get)
{	
	while( ( *get = getchar() ) != '\n' ) 
	{
		get++;
	}

	fflush(stdin);

	return;
}

猜你喜欢

转载自blog.csdn.net/Lth_1571138383/article/details/85232557
今日推荐