华电函数题库

1.编写函数,完成下面的程序,将一个字符串中的字母排序后输出。输出要求:字母从小到大排序(包括大小写)后的字符串。

#include <stdio.h>
#include <string.h>
int sortchar(char s[]);
int main()
{
    char s[80];
    gets(s);
    sortchar(s);
    puts(s);
 
}
int sortchar(char s[])
{
    int a,b,t,k;
    int i=strlen(s);
    char r[80];
    for(a=0;a<i;a++)
    {
        for(b=a;b<i;b++)
        {
            if(s[a]>s[b])
            {
                t=s[a];
                s[a]=s[b];
                s[b]=t;
            }
            t=0;
        }
    }
	for(a=0;a<i;a++)
	 r[a]=s[i-a-1];
}

2.请编一个函数fun,其中n所指存储单元中存放了数组中元素的个数。函数的功能是:删除所有值为y的元素。已在主函数中给数组元素赋值,y的值由主函数通过键盘读入。

#include<stdio.h>
#define M 20
int fun(int aa[],int n, int y);
int main()
{      int aa[M]={1,2,3,3,2,1,1,2,3,4,5,4,3,2,1}, n=15, y, k;
       scanf("%d",&y);
       n = fun(aa, n, y);
      for(k=0; k<n; k++ )
              printf("%d ",aa[k]);
         printf("\n");
return(0);
}
int fun(int aa[],int n, int y)
{
	int k,j;
	for(k=0;k<n; k++)
	{
		if(aa[k]==y)
		{
		for(j=k;j<n-1;j++)
		{
		aa[j]=aa[j+1];
		}
		n--;
		k--;
		}
	}
	return n;
}

3.编写函数 int substring(char s[], char sub[]),查找sub串在字符串s中第一次出现的位置,若找到,函数返回对应位置,否则返回-1。注意主串第1个字符的位置为1。例如s串为“abcdefg”,sub串为“def”,则返回值为4。在main函数中输入主串和子串,调用该函数并输出结果。

#include <stdio.h>
#include <string.h>
int substring(char s[],char sub[]);
int  main(void)
{
    char s[99],sub[99];
    gets(s);
    gets(sub);
    if(substring(s,sub)==0)
 
           printf("%s子串没有找到!",sub);
    else
           printf("子串%s在主串%s中第一次出现的位置是%d",sub,s,substring(s,sub));
}
int substring(char s[],char sub[])
{
    int a,b,c=0,d;
    int n=strlen(sub),m=strlen(s);
    if(strcmp(s,sub)==0)
    return 0;
    else
    {
    	for(a=0;a<m;a++)
        if(s[a]==sub[0])
        {
            for(b=0;b<n;b++)
                if(s[a+b]==sub[b])
                c++;
            if(c==n)             
                    break;
        }
        return a+1;
    }  	
}
 

  1. 编写函数,统计给定字符串中各个字母出现的次数,不区分大小写。在main函数中输入一个字符串,调用该函数进行计算,然后输出统计结果。
#include <stdio.h>
void counta_z(char str[],int count[]);
main()
{     char str[255];
     int count[26]={0};
     int i;
     gets(str);
     counta_z(str,count);
     for(i=0;i<26;i++)
     if(count[i])
          printf("%c出现的次数为:%d\n",i+'a',count[i]);
}
void counta_z(char str[],int count[])
{
	int i,j;
	for(i=0;str[i]!='\0';i++)
	{
		for(j=0;j<27;j++)
		if(str[i]==j+'a'||str[i]==j+'A')
		count[j]++;
	}
}

6.编写一个函数void sort(int a[],int n),实现整型数组元素的升序排列。在main函数中输入数组元素个数和各个元素(不超过10个整数),之后调用sort函数进行排序,在主函数中输出排序后的结果。

#include <stdio.h>
void sort(int a[],int n);
int main()
{
    int a[10],i,n;
    scanf("%d",&n);
    for(i=0;i<n;i++)
          scanf("%d",&a[i]);
    printf("排序之前的数组:");
    for(i=0;i<n;i++)
          printf("%d ",a[i]);
    sort(a,n);
    printf("\n排序之后的数组:");
    for(i=0;i<n;i++)
 
         printf("%d ",a[i]);
 
}
void sort(int a[],int n)
{
	int t,x,z,o;
	for(z=0;z<n-1;z++)
	{
		t=z;
		for(o=z+1;o<n;o++)
		{
			if(a[o]<a[t])
			t=o;
		}
		x=a[t];
		a[t]=a[z];
		a[z]=x;
	}
}

8.编写函数int reverse(int number),它的功能是将number逆序,在main函数中输入一个整数,调用该函数得到逆序的数并输出。例如reverse(11233)的返回值为33211。

#include <stdio.h>
int  reverse(int a);
void main()
{
    int number;
    scanf("%d",&number);
    printf("%d", reverse(number) );

}
int  reverse(int a)
{
	int b;
	while(a)
	{
		b*=10;
		b=b+a%10;
		a/=10;
	}
	return b;
}

9.编写一个函数char my_toupper(char ch),其功能是判断输入字符是否为小写字母,若是,返回其对应的大写字母,否则返回原字符。然后在main函数中输入一个字符,调用该函数得到其大写字母或原字符并输出。

#include <stdio.h>
char my_toupper(char ch);
void main()
{    char ch;

     ch=getchar();

     putchar(my_toupper(ch));

}
char my_toupper(char ch)
{
	if(ch>='a'&&ch<='z')
	return ch-32;
	else 
	return ch; 
}

10.输入1 个正整数 n (1≤n≤6)和n 阶方阵a中的元素,如果a的每一行的最大元素值都相等, 输出"YES", 否则, 输出"NO"。

#include <stdio.h>
#define N 6
int n;
int maxnumber(int a[][N]);
int main()
{
	int c,b;
    int a[6][6];
    scanf("%d",&n);
    for(c=0;c<n;c++)
    for(b=0;b<n;b++)
    scanf("%d", &a[c][b]);
	maxnumber(a);	
 } 
 int maxnumber(int a[][N])
 {
 	int c,d,max=0,i=0;
 	int b[6];
 	for(c=0;c<n;c++)
    {
    for(d=0;d<n;d++)
    {
        if(a[c][d]>max)
        max=a[c][d];
    }
    b[c]=max;        
    }
    for(c=0;c<n;c++)
    {
        if(b[c]==b[c+1])
            i++;
    }
        if(i==n-1)
            printf("YES");
        else
            printf("NO");
 }

11.已知两个整数20和10,编写程序,自定义函数add( )计算这两个数的和,自定义函数sub( )计算这两个数的差,20和10作为参数传递给这两个函数。

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

13.编写1个函数判断一个整数是否左右对称数。 要求函数为: fun(long n)
要求在主程序中输入一个整数,如果该数对称,输出"YES";否则,输出"NO"。
例如 输入:1235321 输出:YES
输入: 1210 输出:NO

#include<stdio.h>
int  fun(long n);
int main(  )
{   long in; 
    scanf("%ld",&in);
     if(fun(in))  
            printf("YES\n");
    else
          printf("NO\n");
 }
 int  fun(long n)
 {
 	long temp1,temp2 = 0;
	temp1 = n;       
	while(n)
	{
	//算法参考第8题
		temp2*=10;//升位数,个位变十位等
		temp2=temp2+n%10;//获取当前n的最后一位,并加在temp2的个位上
		n/=10;//降位数
	}
	if(temp1 == temp2)   
		return 1;
	else
		return 0;
 }
  • 38
    点赞
  • 124
    收藏
    觉得还不错? 一键收藏
  • 15
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值