【C语言每日一题】编程题入门篇-004

一、自定义函数之字符串反转

题目描述

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

输入格式

一行字符

输出格式

逆序后的字符串

样例输入

123456abcdef 

样例输出

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

// 函数声明
void reverseString(char *str);
void removeSpaces(char *str);

int main() {
    char str[100]; // 假设输入不会超过100个字符

    // 输入字符串
    fgets(str, sizeof(str), stdin);

    // 移除字符串中的空格
    removeSpaces(str);

    // 反转字符串
    reverseString(str);

    // 输出反转后的字符串
    printf("%s\n", str);

    return 0;
}

二、自定义函数之字符串连接

题目描述

写一函数,将两个字符串连接

输入格式

两行字符串

输出格式

链接后的字符串

样例输入

123
abc

样例输出

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

int main() {
    char str1[101], str2[101];  // 定义字符数组,并假设每个字符串最大长度为100

    // 读取输入的两个字符串
    fgets(str1, 101, stdin);
    fgets(str2, 101, stdin);

    // 去除每个字符串末尾的换行符
    str1[strcspn(str1, "\n")] = '\0';
    str2[strcspn(str2, "\n")] = '\0';

    // 输出连接后的字符串
    printf("%s%s\n", str1, str2);

    return 0;
}

三、自定义函数之字符提取

题目描述

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

输入格式

一行字符串

输出格式

顺序输出其中的元音字母(aeiou)

样例输入

abcde

样例输出

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

// 函数声明
void extractVowels(const char *input, char *output);

int main() {
    char inputStr[100]; // 输入字符串
    char outputStr[100]; // 存储元音字母的字符串

    // 输入字符串
//    printf("请输入一个字符串:");
    fgets(inputStr, sizeof(inputStr), stdin);
    inputStr[strcspn(inputStr, "\n")] = 0; // 去除换行符

    // 提取元音字母
    extractVowels(inputStr, outputStr);

    // 输出提取的元音字母
    printf("%s\n", outputStr);

    return 0;
}

// 函数定义
void extractVowels(const char *input, char *output) {
    int index = 0; // 用于跟踪输出字符串中的位置

    // 遍历输入字符串中的每个字符
    while (*input) {
        char ch = tolower(*input); // 将字符转换为小写
        if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
            output[index++] = ch; // 如果是元音字母,则添加到输出字符串
        }
        input++; // 指向下一个字符
    }

    // 添加字符串结束符
    output[index] = '\0';
}

四、自定义函数之数字分离

题目描述

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

输入格式

一个四位数

输出格式

增加空格输出

样例输入

1990

样例输出

1 9 9 0
#include <stdio.h>

// 函数声明
void printDigitsWithSpaces(int number);

int main() {
    int number;

    // 输入四位数
//    printf("请输入一个四位数:");
    scanf("%d", &number);

    // 输出每位数字之间带有空格的形式
    printDigitsWithSpaces(number);

    return 0;
}

// 函数定义
void printDigitsWithSpaces(int number) {
    // 分解四位数
    int digit1 = number / 1000;
    int digit2 = (number % 1000) / 100;
    int digit3 = (number % 100) / 10;
    int digit4 = number % 10;

    // 输出每位数字之间带有空格的形式
    printf("%d %d %d %d\n", digit1, digit2, digit3, digit4);
}

五、自定义函数之字符类型统计

题目描述

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

输入格式

一行字符串

输出格式

统计数据,4个数字,空格分开。

样例输入

!@#$%^QWERT    1234567

样例输出

5 7 4 6 
#include <stdio.h>
#include <ctype.h>

// 函数声明
void countCharacters(const char *str, int *letters, int *digits, int *spaces, int *others);

int main() {
    char str[100]; // 假设输入不会超过100个字符
    int letters = 0, digits = 0, spaces = 0, others = 0;

    // 输入字符串
    fgets(str, sizeof(str), stdin);
    str[strcspn(str, "\n")] = 0; // 去除换行符

    // 统计字符类型数量
    countCharacters(str, &letters, &digits, &spaces, &others);

    // 输出统计结果
    printf("%d %d %d %d\n", letters, digits, spaces, others);

    return 0;
}

// 函数定义
void countCharacters(const char *str, int *letters, int *digits, int *spaces, int *others) {
    while (*str) {
        if (isalpha((unsigned char)*str)) {
            (*letters)++;
        } else if (isdigit((unsigned char)*str)) {
            (*digits)++;
        } else if (isspace((unsigned char)*str)) {
            (*spaces)++;
        } else {
            (*others)++;
        }
        str++; // 指向下一个字符
    }
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值