當前位置:編程學習大全網 - 編程軟體 - 編寫程序實現在壹個字符串中查找指定的字符(請用c語言作答)

編寫程序實現在壹個字符串中查找指定的字符(請用c語言作答)

#include<stdio.h>

int main()

{

int i,index,count;

char a,ch,str[80];

scanf("%c\n",&a);

i=0;

index=-1;

count=0;

ch=getchar();

for(i=0;ch!='\n';i++){

str<i>=ch;

count++;

ch=getchar();

}

for(i=0;i&lt;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&lt;stdio.h&gt;

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())!='\n')

printf("%c",c);

return 0;

}

註:可以利用getchar()函數讓程序調試運行結束後等待編程者按下鍵盤才返回編輯界面,用法:在主函數結尾,return 0;之前加上getchar();

  • 上一篇:軟件工程屬於什麽專業類
  • 下一篇:美國學者傑裏·本特利所著《新全球史》寫道:“19世紀下半葉,受進化論和科學分類學的影響,西方史學家傾
  • copyright 2024編程學習大全網