创建一个名为count的函数,用来判断用户输入的字符串中“.”(句号)的个数并返回
int count(char *p){
char *s = p;
int cnt = 0;
while(*s != '\0'){
if(*s == '.'){
cnt ++;
}
s ++;
}
return cnt;
}
遍历字符串比较每个字符是否为 .,计数就行了
#include<stdio.h>
#include <string.h>
int count(char t[])
{
int j,c=0;
int n=strlen(t);
for(j=0; j<n; j++)
{
if(t[j]=='.')
{
c++;
}
}
return c;
}
int main()
{
char t[250];
printf("输入字符串:");
gets(t);
printf("'.'的个数为:%d",count(t));
return 0;
}
#include <stdio.h>
#define N 256
int count(const char *s)
{
int n = 0;
while (*s)
if (*s++ == '.')
n++;
return n;
}
int main()
{
char a[N];
fgets(a, N, stdin);
printf("%d", count(a));
return 0;
}
#include<stdio.h>
int count(char str[])
{
int i=0;
int res=0;
while (str[i] != '\0') {
if (str[i]=='.')
{
res++;
}
i++;
}
return res;
}
int main()
{
char str[1024] = {0};
gets_s(str);
int c = count(str);
printf("%d\n", c);
return 0;
}