C语言网刷题记录(31-37)

1031

题目:
写一函数,使输入的一个字符串按反序存放,在主函数中输入并输出反序后的字符串(不包含空格)。
代码:

#include<string.h>
int main() {
    char c[1000];
    int n,i;
    gets(c);
    n = strlen(c);
    for(i=n-1; i>=0; i--) {
        printf("%c",c[i]);
    }
    return 0;
}

1032

题目:
写一函数,将两个字符串连接
代码:

#include<stdio.h>
#include<string.h>
int main() {
    char a[100],b[100],c[100];
    int i,j,m,n;
    gets(a);
    gets(b);
    m = strlen(a);
    n = strlen(b);
    for(i=0; i<m; i++) {
        c[i] = a[i];
    }
    for(j=0; j<n; j++) {
        c[m+j] = b[j];
    }
    puts(c);
    return 0;
}

1033

题目:
写一函数,将一个字符串中的元音字母复制到另一个字符串,然后输出。
代码:

#include<stdio.h>
#include<string.h>
int main() {
    char m[100];
    int c,i;
    gets(m);
    c=strlen(m);
    for(i=0; i<c; i++) {
        if(m[i]=='a')
        {
            printf("%c",m[i]);             
        }
        if(m[i]=='A')
        {
            printf("%c",m[i]);
        }
        if(m[i]=='e')
        {
            printf("%c",m[i]);
        }
        if(m[i]=='E')
        {
            printf("%c",m[i]);
        }
        if(m[i]=='i')
        {
            printf("%c",m[i]);
        }
        if(m[i]=='I')
        {
            printf("%c",m[i]);
        }
        if(m[i]=='o')
        {
            printf("%c",m[i]);
        }
        if(m[i]=='O')
        {
            printf("%c",m[i]);
        }
        if(m[i]=='u')
        {
            printf("%c",m[i]);
        }
        if(m[i]=='U')
        {
            printf("%c",m[i]);
        }
    }
    return 0;
}

1034

题目:
写一函数,输入一个四位数字,要求输出这四个数字字符,但每两个数字间空格。如输入1990,应输出"1 9 9 0"。
代码:

#include <stdio.h>
void func(int value)
{
    int num[4]; //定义一个数组存放这个整数的4个数字
    int i = 0;
    while (value / 10 != 0)
    {
        num[i++] = value % 10; //取出该整数的每一位存入数组,注意,这是从个位开始存放的
        value = value / 10;
    }
    num[i] = value; //不要忘了最后一个数字
                     //倒序输出数组中的数字
    for (i = 3; i >= 0; i--)
    {
        printf("%d", num[i]);
        if (i != 0) //最后一位数字后面没有空格
            printf(" ");
    }
}
 
int main()
{
    int value;
    scanf("%d", &value);
    func(value);
    return 0;
}

1035

题目:
编写一函数,由实参传来一个字符串,统计此字符串中字母、数字、空格和其它字符的个数,在主函数中输入字符串以及输出上述结果。 只要结果,别输出什么提示信息。
代码:

#include<stdio.h>
#include<string.h>
 
void statistics(char *str, int *num_char, int *num_num, int *space_num, int *other_num)
{
    int len=0, i=0;
    len = strlen(str);
    *num_char = 0;
    *num_num = 0;
    *space_num = 0;
    *other_num = 0;
    for(i = 0; i < len; i++)
    {
        if(str[i]>='0' && str[i]<='9')
            *num_num+=1;
        else if( (str[i]>='a' && str[i]<= 'z') || (str[i]>='A' && str[i]<='Z'))
            *num_char+=1;
        else if(str[i]==' ')
            *space_num+=1;
        else
            *other_num+=1;
    }
}
 
int main()
{
    char Mystring[1024];
    int num_char, num_num, space_num, other_num;
    scanf("%[^\n]", Mystring);
    statistics(Mystring, &num_char, &num_num, &space_num, &other_num);
    printf("%d %d %d %d\n", num_char, num_num, space_num, other_num);
    return 0;
}

1036

题目:
定义一个带参的宏,使两个参数的值互换,并写出程序,输入两个数作为使用宏时的实参。输出已交换后的两个值。
代码:

#include <stdio.h>
#define change(a,b) \
    a=a+b;\
    b=a-b;\
    a=a-b;
int main()
{
    int a,b;
    scanf("%d%d",&a,&b);
    change(a,b);
    printf("%d %d",a,b);
    return 0; 
}

1037

题目:
输入两个整数,求他们相除的余数。用带参的宏来实现,编程序。
代码:

    #include <stdio.h>
    #define e a%b
    int main(int argc, const char* argv[])
    {
        int a, b;
        scanf("%d%d", &a, &b);
        printf("%d", e);
        return 0;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值