六、字符串(10道抽1道,写代码)
1、编写程序,用你的英文名字初始化一个字符串,然后输出,如John (较易)
#include<stdio.h>#include<string.h>void main(){
char name[10]={"john"};puts(name);
}
2、请定义一个长度为10的字符串,并将其输出显示。(较易)#include<stdio.h>#include<string.h>void main(){
char name[10]={"johnghasd"};puts(name);
}
3、定义2串密码,判断该两串密码是否一致。(较难)#include<stdio.h>#include<string.h>void main(){
char s1[10]={"johnghasd"};char s2[10]={"gaoshglks"};if(strcmp(s1,s2)==0)printf("一致\n");else
printf("不一致\n");
}
4、反向加密:定义任意长度的密码,输出反向的密码。(较易)
5、判断字符串是否为回文串。(提示:回文字符串是具有回文特性的字符串:即该字
符串从左向右读和从右向左读一样,如12321)(难)6、将字符串中的小写字母改为相应的大写字母 。(难)#include<stdio.h>#include<string.h>void main(){
char str[10]={"bcaefd"};int i;
for(i=0;str[i]!='\0';i++)
if(str[i]>='a'&&str[i]<='z')
str[i]-=32;
4 11 201628
puts(str);
}
7、定义两个字符串,比较两个字符串的长度,输出较长的字符串。若两个字符串长度
相同,则输出第一个字符串 。(较难)
#include<stdio.h>#include<string.h>void main(){
char str1[10]={"bcaefd"};char str2[10]={"gfsdhf"};int i,j;
i=strlen(str1);j=strlen(str2);if(i<j)
puts(str2);else
puts(str1);
}
8、定义一字符串,输出字符串中第一个出现字母a 的位置,如没有则输出-1 。(难)#include<stdio.h>void main(){
char str[100]={"bca452adefb543s53d"};int i,n=0;
for(i=0;str[i]!='\0';i++){
if(str[i]=='a')
n=i;if(n>0)
break;}
printf("%d\n",n+1);
}
9、定义一字符串,统计该字符串中数字的个数。(较难)#include<stdio.h>void main(){
char str[100]={"abc452defb543s53d"};int i,n=0;
for(i=0;str[i]!='\0';i++)
if(str[i]>='0'&&str[i]<='9')
n++;
printf("%d\n",n);
}
4 11 201628
10、定义两个字符串,合并成一个新的字符串并输出。(较难)#include<stdio.h>#include<string.h>void main(){
char str1[100]={"abcdefbsd"};char str2[]={"3421sd"};
printf("%s\n",strcat(str1,str2));
}
七、函数(抽1道,写代码)
注:在分支结构中任抽一题,要求用函数实现。(较难)
八、指针(10道抽1道,写代码)
11、
定义函数求2个整数的最大值(要求使用指针变量作函数参数来实现)。(较难)
P226
#include<stdio.h>
int main(){
void swap(int *p1,int *p2);int a,b;
int *p1,*p2;
printf("请输入两个整数给a b :\n");scanf("%d %d",&a,&b);p1=&a;p2=&b;if(a<b)
swap(p1,p2);
printf("max=%d\n",a);return 0;
}
void swap(int *p1,int *p2){
int temp;temp=*p1;*p1=*p2;*p2=temp;}
12、定义函数实现两个float 型数的交换(要求使用指针变量作函数参数来实现)(较
4 11 201628c++求字符串长度
难)
#include<stdio.h>int main(){
void swap(float *p1,float *p2);float a,b;
float *p1,*p2;
printf("请输入两个数给a b :\n");scanf("%f %f",&a,&b);p1=&a;p2=&b;
swap(p1,p2);
printf("a=%0.2f b=%0.2f\n",a,b);return 0;
}
void swap(float *p1,float *p2){
float temp;temp=*p1;*p1=*p2;*p2=temp;}
13、定义(返回指针)函数求三个数的最大值。(较难)
#include<stdio.h>int main(){
int a,b,c;
int *max(int x,int y,int z);
scanf("%d%d%d",&a,&b,&c);printf("%d\n",*max(a,b,c));
}
int *max(int x,int y,int z){
int f,*q;f=x;if(x<y){
f=y;}
if(y<z){
f=z;}
4 11 201628
q=&f;return q;
}
14、定义(返回指针)函数求三个数的和。(较难)
#include<stdio.h>int main(){
int a,b,c;
int *max(int x,int y,int z);
scanf("%d%d%d",&a,&b,&c);printf("%d\n",*max(a,b,c));}
int *max(int x,int y,int z){
int f,*q;f=x+y+z;q=&f;return q;}
15、定义函数求x ,y 中的较小数(要求用指向函数的指针实现)。(难)
P269
#include<stdio.h>int main(){
int min(int,int);int (*p)(int,int);int a,b,c;(*p)=min;
scanf("%d %d",&a,&b);c=(*p)(a,b);
printf("min=%d\n",c);return 0;}
int min(int x,int y){
if(x>y)
return y;else
return x;
}
16、定义函数求x ,y 中的较大数(要求用指向函数的指针实现)。(难)
#include<stdio.h>int main()
4 11 201628
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论