中国 外尔费米子:用指针做的,出了点问题~!!!

来源:百度文库 编辑:杭州交通信息网 时间:2024/05/07 13:25:46
用指针导入函数做这个题我会,但不用函数也用指针有点迷糊了~!!
#include<stdio.h>
#define N 20
void main()
{
char str[N],a,*p=0,count=0;
printf("输入一串字符:");
gets(str);
printf("输入要查找的字符:");
scanf("%c",&a);
*p=str;
while(*p!='\0')
{
if(*p==a)
{
count++;
printf("%c在第%d位出现\n",a,p+1);
}
p++;
}
printf("%c在该字符串中出现%d次\n",a,count);
}

#include<stdio.h>
#include<string.h>
#define N 20
void main()
{
char str[N], a, *p = NULL;
int n = 0, count = 0;
printf("输入一串字符:");
gets(str);
printf("输入要查找的字符:");
scanf("%c", &a);
p = str; //应该传地址而不是值
while(*p != '\0')
{
n++;
if(*p == a)
{
count++;
printf("%c在第%d位出现\n", a, n);
}
p++;
}
printf("%c在该字符串中出现%d次\n",a,count);
}

#include<stdio.h>
#include<string.h>
#define N 20
void main()
{
char str[N], a, *p = NULL;
int n = 0, count = 0;
printf("输入一串字符:");
gets(str);
printf("输入要查找的字符:");
scanf("%c", &a);
p = str;
while(*p != '\0')
{
n++;
if(*p == a)
{
count++;
printf("%c在第%d位出现\n", a, n);
}
p++;
}
printf("%c在该字符串中出现%d次\n",a,count);
}