设计一个函数,在字符串str中查找出字符ch出现的位置并返回字符出现的次数
1个回答
展开全部
#include
#include
int
search(char
*
str,char
ch,int
*
loc){
int
i;
for(i=0;str[i]
!=
'\0';
i++){
/*查找是否有匹配的字符*/
if(str[i]
==
ch){
*loc
=
i+1;
return
1;
}
}
*loc
=
-1;
return
0;
}
main(){
char
*string,
ch;
int
*loc
=
0,re
=0;
printf("please
input
a
string:");
scanf("%s%c",string);
/*%c用来吸收回车*/
printf("please
input
the
character
for
search:");
scanf("%c",&ch);
/*或者ch=getchar();*/
re
=
search(string,ch,loc);
printf("The
return
value
is
%d,The
location
of
the
character
in
the
string
is
%d",re,*loc);
getch();
}
#include
int
search(char
*
str,char
ch,int
*
loc){
int
i;
for(i=0;str[i]
!=
'\0';
i++){
/*查找是否有匹配的字符*/
if(str[i]
==
ch){
*loc
=
i+1;
return
1;
}
}
*loc
=
-1;
return
0;
}
main(){
char
*string,
ch;
int
*loc
=
0,re
=0;
printf("please
input
a
string:");
scanf("%s%c",string);
/*%c用来吸收回车*/
printf("please
input
the
character
for
search:");
scanf("%c",&ch);
/*或者ch=getchar();*/
re
=
search(string,ch,loc);
printf("The
return
value
is
%d,The
location
of
the
character
in
the
string
is
%d",re,*loc);
getch();
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询