天天看点

Educoder_C语言_第六部分 数组

第1关 字符逆序

#include<stdio.h>
#include<string.h>
int main(void)
{
    /*********Begin*********/
char str[81],temp;
	int i,j;

	gets(str);
	i=0;
	j=strlen(str)-1;
	while(i<j)
	{
		temp=str[i];
		str[i]=str[j];
		str[j]=temp;
		i++;
		j--;
	}
	printf("%s",str);

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

第2关 字符统计

#include<stdio.h>
#include<string.h>
int main(void)
{
    /*********Begin*********/
    int n,count,i,j,k,w;
    char a[100];
    scanf("%d",&n);
    w=n;
    int b[100]={0};
    while(n > 0)
    {
        count=0;
        scanf("%s",a);
        k=strlen(a);
        for(i=0;i<k;i++)
        {
            if(a[i]<='9'&&a[i]>='0')
            {
                count++;
            }
        }
        b[n-1]=count;
        n--;
    }
    for(j=w-1;j>=0;j--)
    {
        printf("%d",b[j]);
        if(j>0)
        printf("\n");
    }
    /*********End**********/
    return 0;
}           

第3关 字符插入

#include <stdio.h>
#include <string.h>
int main(void)
{
    /*********Begin*********/
char a[101], b[100];
 int i = 0, j = 0, min, max, x, y;
 min = 0;//min在下面(if (a[i]<a[min]))作为右值被调用,要先做初始化
 max = 0;//初始化
scanf("%s",a);
scanf("%s",b);
 x = strlen(a);
 y = strlen(b);
 for (i = 0; i<x; i++)
 {
  if (a[i]<a[min])
   min = i;
 }
 for (j = 0; j<y; j++)
 {
  if (b[j]>b[max])
   max = j;
 }
  
 for (i = x; i >min; i--)//for循环注意其语句执行先后顺序,判断条件语句(i >min),
 {                       //满足之后先执行for循环体语句(a[i] = a[i-1];),再执行i--。
  a[i] = a[i-1];      
 } 
 if (min == x)
  a[i] = b[max];//这里原程序直接把整数max赋给了a[i]
 else
 {
  a[i +1] = b[max];
 }
 a[x + 1] = '\0';//在字符串最后添加字符串结束符
printf("%s",a);

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

第4关 字符串处理

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(void)
{
    /*********Begin*********/
char t[100],s[100];
    int i,j,pos;
    gets(t);
    gets(s);
    scanf("%d",&pos);
    for(i=0;i<strlen(t);i++)
    {
        printf("%c",t[i]);
        if(i+1==pos)
        {
            for(j=0;j<strlen(s);j++)
                printf("%c",s[j]);
        }
    }

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

第5关 字符串统计

#include<stdio.h>
#include <string.h>
int main(void)
{
    /*********Begin*********/
char a[100];
    int i, j, pos = 0;
    int str_len, word_len, max_word_len;
 
    while(1) {
        str_len = word_len = max_word_len = 0;
 
         fgets(a, 100, stdin);//fgets函数的用法
 
        if (strlen(a) <= 1)//输入的字符只有一个的情况
            continue;
        if (strlen(a) < 99)   //remove '\n'
            a[strlen(a)-1] = 0;
 
        if(strncmp(a,"stop", strlen("stop"))==0)
            break;
        for(i = 0; a[i] !='\0'; i++) {
            if(a[i] != ' ') {
                word_len++;
                str_len++;
                continue;
            }
            if (word_len  > max_word_len) {
                max_word_len = word_len;
                pos = i - word_len;
            }
            word_len = 0;
        }
        if (word_len  > max_word_len) {
            max_word_len = word_len;
            pos = i - word_len;
        }
 
        printf("%d ", str_len);
        for (i = pos; i < pos + max_word_len; i++)
            printf("%c", a[i]);
        putchar(10);
    }
 

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

第6关 字符串排序

#include<stdio.h>
#include<string.h>
int main(void)
{
    /*********Begin*********/
 char a[30],b[30],c[30],max[30];
    scanf("%s %s %s",&a,&b,&c);
    if(strcmp(a,b)>0)
    {
        strcpy(max,a);
        strcpy(a,b);
        strcpy(b,max);
    }
    if(strcmp(a,c)>0)
    {
        strcpy(max,a);
        strcpy(a,c);
        strcpy(c,max);
    }
    if(strcmp(b,c)>0)
    {
        strcpy(max,b);
        strcpy(b,c);
        strcpy(c,max);
    }
    printf("%s\n%s\n%s",a,b,c);

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

继续阅读