當前位置:編程學習大全網 - 編程軟體 - 請編寫壹個函數int func(char *str,char ch),它的功能是:求出str字符串

請編寫壹個函數int func(char *str,char ch),它的功能是:求出str字符串

#include<stdio.h>

#include<string.h>

int func(char *str,char ch)

{

int i;

for(i=0;*str;str++)

if(*str==ch)i++;

return i;

}

int main()

{

int find;

char a[80];

printf("輸入字符串\n");

gets(a);

find=func(a,'c');

printf("%d",find);

}

2.

#include<stdio.h>

int func(char *str,char ch)

{

int i = 0;

for(char *p=str;*p;p++)

if(*p==ch) i++;

return i;

}

int main()

{

int find;

char a[80];

printf("輸入字符串\n");

gets(a);

find=func(a,'c');

printf("%d",find);

}

3.統計字符串首次出現字符c時所在位置

#include<stdio.h>

#include<string.h>

char?str[100];//全局變量?

int?getchr(char?str[],char?ch)

{

int?count=0;

for(int?i=0;str[i]!='\0';i++)

if(str[i]!=ch)?

count++;

else?{count++;break;}

return?count;//當字符串沒有字符c時,返回的count值等於字符串長l?

}

int?main()

{

int?find,l;

char?a[80];

printf("輸入字符串\n");

gets(a);

l=strlen(a);

find=getchr(a,'c');

if(str[l]!='c'&&find==l)

find=0;//當字符串沒有字符c時,返回的count值等於字符串長l?,將find置0

printf("字符c第壹次出現在字符串中的位置\nfind=%d,字符串長l=%d",find,l);

}

  • 上一篇:王正陽是如何學習編程的?
  • 下一篇:javascript form.submit()有無回調函數,可以返回壹段信息。
  • copyright 2024編程學習大全網