0
点赞
收藏
分享

微信扫一扫

头歌答案(字符串基本操作)

以后还会持续更新头歌答案,并会有详细解析哦,有疑惑可以留言啊

第一关:字符逆序

#include<stdio.h>
#include<string.h>
int main()
{
    char ch[1000];
    scanf("%s", &ch);
    int j= 0;
    for(int i = 0; ; i ++){
        if(ch[i] != '\0') j++;//记录字符串长度
        else break;
    }
    for(int i = j - 1; i >= 0; i --)
        printf("%c", ch[i]);//逆序输出即可
    return 0;
}

第二关:字符统计

#include<stdio.h>
int main(){
    int n;
    scanf("%d", &n);
    while(n --){
        char s[10000];
        scanf("%s", s);输入字符串是不需要取址符的!
        int cnt = 0;
        for(int i = 0; s[i] != '\0'; i ++){
            if(s[i] >= '0' && s[i] <= '9')//注意字符1!=数字1.字符与数字无法比较
                cnt ++;//计数
        }
        printf("%d\n", cnt);
    }
    return 0;
}

第三关:字符插入

#include <stdio.h>
#include <string.h>//没写就报错
int main()
{
    char a[10000],b[10000];
    scanf("%s %s", a, b);
    int n1 = strlen(a),n2 = strlen(b),MIN = 1e+5,MAX = -1e+5;
    for(int i = 0; i < n1; i ++){
        if(MIN > a[i])  MIN = a[i];//把a中最小值找到
    }
    for(int i = 0; i < n2; i ++){
        if(MAX < b[i])  MAX = b[i];//把b中最大值找到
    }
    for(int i = 0; i < n1; i ++){
        printf("%c", a[i]);
        if(a[i] == MIN)//把最大值插到最小值后面
        printf("%c", MAX);
    }
}
举报

相关推荐

0 条评论