A. 編寫程序實現在一個字元串中查找指定的字元(請用c語言作答)
#include<stdio.h>
int main()
{
int i,index,count;
char a,ch,str[80];
scanf("%c ",&a);
i=0;
index=-1;
count=0;
ch=getchar();
for(i=0;ch!=' ';i++){
str<i>=ch;
count++;
ch=getchar();
}
for(i=0;i<count;i++)
if(a==str<i>)
index=i;
if(index!=-1)
printf("index=%d",index);
else
printf("Not Found");
return 0;
}
getchar()用法:
getchar()函數的作用是從計算機終端(一般為鍵盤)輸入一個字元。getchar()函數只能接收一個字元,其函數值就是從輸入設備得到的字元。
例:
#include<stdio.h>
int main(void)
{
int c;
/*Note that getchar reads from stdin and
is line buffered;this means it will
not return until you press ENTER.*/
while((c=getchar())!=' ')
printf("%c",c);
return 0;
}
註:可以利用getchar()函數讓程序調試運行結束後等待編程者按下鍵盤才返回編輯界面,用法:在主函數結尾,return 0;之前加上getchar();