版权声明:转载请注明来源~ https://blog.csdn.net/Lth_1571138383/article/details/85246949
Github地址: φ(>ω<*)这里这里。
/*
本次任务跟任务5(11章编程练习5)基本相同,不过这次只要返回一个非零值好了。。
ps:其实这种题目都会了,我能不能改一个函数名就算完成了╮(╯﹏╰)╭
*/
#include<stdio.h>
#define o 100
int is_within(char chr, char * orig);
void get(char * get);
int main(void)
{
int place1 = 0;
char orig[o] = {};
char find = 0;
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 a true value.\n\n"); // 英文不过小学水平不要介意。。大概意思知道就行。
while( quit != 'q' )
{
printf("Now, Please input string:\n");
get(orig);
printf("\nNow, Please input character: ");
scanf("%c", &find);
fflush(stdin);
place1 = is_within(find, orig);
if( place1 != 0 )
{
printf("Find that character, return subscript %d. Do you want to quit "
"or try again ?(Enter 'q' to quit) :",place1);
scanf("%c", &quit);
fflush(stdin);
}
else
{
printf("We don't find it, return 0. Do you want to quit or try again ?(Enter 'q' to quit) : ");
scanf("%c", &quit);
fflush(stdin);
}
}
printf("\nBye ~\n");
getchar();
return 0;
}
int is_within(char chr, char * orig)
{
int i = 0; // 这个变量在下面的代码中并没有任何卵用。只是。。
// 要返回一个非零值,我就返回指定字符在字符串中的下标好了。。。。
int place2 = 0;
while(*orig != '\n' && *orig != EOF)
{
place2 = (*orig == chr ? i : 0 );
if( place2 == 0)
{
orig++;
i++;
}
else
{
break;
}
}
return place2;
}
void get(char * get)
{
while( ( *get = getchar() ) != '\n' )
{
get++;
}
fflush(stdin);
return;
}