Pta:实验四:查找指定字符

实验4: 7-2 查找指定字符

输入一个字符 char ch

输入一串字符串 chat str[100]

int index,flog,i;

index 存放下标

flog 用 0,1两种状态判定在字符串中是否存在需要查找的字符

代码如下:

#include
int main(){
    char ch, str[100];
	int index, flog=0, i;
	scanf("%c\n", &ch);  //注意必须加回车符\n  注①
	gets(str);
	for (i = 0; str[i] != '\0'; i++) {
	if (str[i] == ch) {
		flog = 1;
		index = i;
			}
		}
	if (flog) {
	printf("index = %d", index);

	}
	else {
	printf("Not Found");
	}
	return 0;
}

①:输入m后有一个回车,而gets语句是以回车作为结束的标志,若不加回车符\n在scanf前,则gets处得到的字符串为空,所以加一个\n把回车吃掉。

运行结果:
Pta:实验四:查找指定字符_第1张图片

你可能感兴趣的:(pta学习笔记,笔记)