编写程序,输入一字符串,输出指定字符在字符串中出现的位置和次数;如果该字符串中不包含指定字符,输出相应的提示信息。

编写程序,输入一字符串,输出指定字符在字符串中出现的位置和次数;如果该字符串中不包含指定字符,输出相应的提示信息。

例如:字符串aevdbcg中字符b出现的位置为4,次数为1;

          字符串aaaab中字符a出现的位置为0,1,2,3,次数为4

编程提示:

(1)定义两个一维数组,一个字符数组用来存放字符串,一个整型数组用来存放指定字符出现的为位置。

(2)定义一个标志量flag初始化为0,用来记录字符串中是否有指定字符。

(3)依次访问字符串中的字符和指定字符作比较,如果相同就把下标存入整型数组,并置flag为1。

说明:

(1)输入格式:getchar(),gets()

 (2)提示性文字输出格式:"input a charactor and a string:"

       输出格式:"number=%d\npositions are:"

                         "%5d"

                         "no charactor %c\n"

#include<stdio.h>

#include<string.h>

int main()

{

    char a[100];

    int b[100];

    char ch;

    int flag = 0;

    int n = 0;

    int len = 100, i = 0;

    printf("input a charactor and a string:");

    ch = getchar();

    gets_s(a,100);

    len = strlen(a);

    for (i = 0; i < len; i++)

        if (a[i] == ch) {

            b[n] = i;

            flag = 1;

            n++;

        }

    if (flag == 0)

        printf("no charactor %c\n", ch);

    else {

        printf("number=%d\npositions are:", n);

        for (i = 0; i < n; i++)

            printf("%5d", b[i]);

    }

    return 0;

}

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值