C语言编程小练习

1.输入一个字符串,计算字符串中子串出现的次数

#include <stdio.h>
#include <string.h>

#define MAX_SIZE 30

int main()
{
    int len;
    int i;
    int j;
    int flag = 0;
    int count = 0;

    char str1[MAX_SIZE];
    char str2[MAX_SIZE];
    char str_temp[MAX_SIZE];

    printf("Please input a string:\n");
    scanf("%s",str1);
    printf("Please input another string:\n");
    scanf("%s",str2);

    len = strlen(str2);

    for(i = 0; str1[i] != '\0'; i++)
    {
        for(j = 0; j < len; j++)
        {
            str_temp[j] = str1[i + j];
        }
        if(strcmp(str_temp, str2) == 0)
        {
            count++;
        }
    }

    printf("\nThe number is:%d\n",count);

    return 0;
}

2.有n个人围成一圈,顺序排号,从第一个开始报数(从1到3报数),凡报到3的人退出圈子,问最后最后留下的是原来第几号的那位.

#include <stdio.h>
#define MAX_SIZE 1024

int main()
{
    int i;
    int num;
    int count = 0;
    int temp;
    int a[MAX_SIZE] = {0};

    printf("Please input n:");
    scanf("%d",&num);

    temp = num;

    for(i = 0; i < num; i++)
    {
        a[i] = i+1;
    }

    i = 0;

    while(num > 1)
    {
        if(a[i] != 0)
        {
            count++;
        }

        if(count == 3)
        {
            a[i] = 0;
            num--;
            count = 0;
        }

        i++;

        if(i == temp)  //如何做到循环遍历这个数组,当i到末尾的时候再将它置为0,就又到了数组开头。
        {
            i = 0;
        }
    }

    for(i = 0; i < temp; i++)
    {
        if(a[i] != 0)
        {
            printf("the result is:%d\n",a[i]);
            break;
        }
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值