头歌平台 字符数组

#第1关:字符逆序

任务描述
题目描述:输入一个字符串,输出反序后的字符串。

相关知识(略)
编程要求
请仔细阅读右侧代码,结合相关知识,在Begin-End区域内进行代码补充。
输入
一行字符
输出
逆序后的字符串

测试说明
样例输入:
123456abcdef
样例输出:
fedcba654321

特别注意:样例输出没有进行换行操作

#include<stdio.h>
int main(void)
{
    /*********Begin*********/
char a[1000],b[1000];
int i,j,n=0;
gets(a);
for(i=0;a[i]!='\0';i++)
    n++;
for(i=0,j=n-1;a[i]!='\0';i++,j--)
    b[j]=a[i];
for(i=0;a[i]!='\0';i++)
printf("%c",b[i]);     
    /*********End**********/
    return 0;
}

#第2关:字符统计

任务描述
题目描述:对于给定的一个字符串,统计其中数字字符出现的次数。

相关知识(略)
编程要求
请仔细阅读右侧代码,结合相关知识,在Begin-End区域内进行代码补充。
输入
输入数据有多行,第一行是一个整数n,表示测试实例的个数,后面跟着n行,每行包括一个由字母和数字组成的字符串。
输出
对于每个测试实例,输出该串中数值的个数,每个输出占一行。、

测试说明
样例输入:
2
asdfasdf123123asdfasdf
asdf111111111asdfasdfasdf
样例输出:
6
9

#include<stdio.h>
int main(void)
{
    /*********Begin*********/
int i,j,n,x=0;
scanf("%d",&n);
char a[n][100];
for(i=0;i<n;i++)
   scanf("%s",a[i]);
for(i=0;i<n;i++)
{
    for(j=0;a[i][j]!='\0';j++)
    {
        if(a[i][j]>='0'&&a[i][j]<='9')
        x++;
    }
    printf("%d\n",x);
    x=0;
}

    /*********End**********/
    return 0;
}

#第3关:字符插入

任务描述
题目描述:输入两个字符串a和b,将b串中的最大字符插入到a串中最小字符后面。

相关知识(略)
编程要求
请仔细阅读右侧代码,结合相关知识,在Begin-End区域内进行代码补充。
输入
输入一段文字
输出
输入两个字符串a和b。

测试说明
样例输入:
MynameisAmy
MynameisJane
样例输出:
MynameisAymy
提示:
字符串长度不超过100

注意:使用**gets()**函数会引起警告并不是报错,只要代码编译正确并不会影响测评结果。
推荐使用:fgets()函数。

#include <stdio.h>
#include <string.h>
int main(void)
{
    /*********Begin*********/
char s1[100],s2[100];
scanf("%s%s",s1,s2);

int i,j,m1=0,m2=0;

char min1=s1[0],max2=s2[0];

for(i=1;s1[i];i++)
if(s1[i]<min1)
min1=s1[i],m1=i;

for(i=1;s2[i];i++)
if(s2[i]>max2)
max2=s2[i],m2=i;

for(i=0;i<=m1;i++)
putchar(s1[i]);
putchar(s2[m2]);

for(i=m1+1;s1[i];i++)
putchar(s1[i]);
putchar('\n');

/*********End**********/
    return 0;
}

#第4关:字符串处理

任务描述
题目描述:编写程序,输入字符串s1和s2以及插入位置f,在字符串s1中的指定位置f处插入字符串s2。如输入"BEIJING", "123", 3,则输出:"BEI123JING"。

相关知识(略)
编程要求
请仔细阅读右侧代码,结合相关知识,在Begin-End区域内进行代码补充。
输入
第一行和第二行分别输入两个字符串s1和s2,第三行输入插入位置f。

每个字符串的长度不超过100个字符。
输出
输出一行插入后的字符串。

测试说明
样例输入:
BEIJING
123
3
样例输出:
BEI123JING

注意:使用**gets()**函数会引起警告并不是报错,只要代码编译正确并不会影响测评结果。
推荐使用:fgets()函数。

#include<stdio.h>
int main(void)
{
    /*********Begin*********/
#include<string.h>
char s1[1024], s2[1024];
int n;
 
    scanf("%s%s%d", s1, s2, &n);
 
    strcat(s2, s1 + n);
    strcpy(s1 + n, s2);
 
    printf("%s\n", s1);
    /*********End**********/
    return 0;
}

#第5关:字符串统计

任务描述
题目描述:输入一段字符(由空格、字母和数字几种组成,保证开头不为空格),里面有若干个字符串,求这些字符串的长度和,并输出最长字符串内容,如果有多个输出最先出现的那个字符串。以stop作为最后输入的字符串。

相关知识(略)
编程要求
请仔细阅读右侧代码,结合相关知识,在Begin-End区域内进行代码补充。
输入
输入一段文字
输出
输入一段字符,以stop作为最后输入的字符串。

测试说明
样例输入:
My name is Amy
My name is Jane
stop
样例输出:
11 name
12 name
提示:
字符串长度不超过100。

注意:使用gets()函数会引起警告,但正确使用不影响测评结果。
推荐使用:fgets()函数。

#include<stdio.h>
#include <string.h>
int main(void)
{
    /*********Begin*********/
int sumLength = 0, partLength = 0;
    char arr[1000];
    int max = 0;
    char Temp_Arr[1000];
    gets(arr);
    int i, j;
    while (strcmp(arr, "stop") != 0)
    {
        for (i = 0; i < strlen(arr); i++)
        {
            if (arr[i] != ' ')
            {
                sumLength++;
                partLength++;
            }
            else
            {
                if (partLength > max)
                {
                    max = partLength;
                }
                partLength = 0;
            }
        }
        if (partLength > max)
        {
            max = partLength;
        }

        partLength = 0;
        for (i = 0; i < strlen(arr); i++)
        {
            if (arr[i] != ' ')
            {
                partLength++;
                if (partLength == max)
                {
                    for (j = 0; j < max; j++)
                    {
                        Temp_Arr[j] = arr[j + i - max+1];
                    }
                    Temp_Arr[max] = '\0';
                    break;
                }
            }
            else
            {             
                partLength = 0;
            }
        }
        printf("%d %s", sumLength, Temp_Arr);
        printf("\n");
        sumLength = 0;
        partLength = 0;
        max = 0;
        gets(arr);
    }

    /*********End**********/
    return 0;
}





















#第6关:字符串排序

任务描述
题目描述:输入3行,每行n个字符串,按由小到大的顺序输出

相关知识(略)
编程要求
请仔细阅读右侧代码,结合相关知识,在Begin-End区域内进行代码补充。
输入
3行字符串
输出
按照从小到大输出成3行

测试说明
样例输入:
cde
afg
abc
样例输出:
abc
afg
cde

#include<stdio.h>
#include<string.h>
int main(void)
{
    /*********Begin*********/
char a[100],b[100],c[100],t[100];
int x,y,z;
scanf("%s%s%s",a,b,c);
x=strcmp(a,b);
y=strcmp(a,c);
z=strcmp(b,c);
if(x>0&&y>0&&z>0)
  printf("%s\n%s\n%s",c,b,a);
if(x>0&&y>0&&z<0)
  printf("%s\n%s\n%s",b,c,a);
if(x<0&&y>0&&z>0)
  printf("%s\n%s\n%s",c,a,b);
if(x<0&&y<0&&z>0)
  printf("%s\n%s\n%s",a,c,b);
if(x>0&&y<0&&z<0)
  printf("%s\n%s\n%s",b,a,c);
if(x<0&&y<0&&z<0)
  printf("%s\n%s\n%s",a,b,c);         

    /*********End**********/
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值