admin 管理员组

文章数量: 1184232


2024年3月21日发(作者:handclap)

printf("%dt",a[i][j]);

}

printf("n");

for(k=0;k

【运行结果】

a[0][0]=1

a[0][1]=2

a[0][2]=3

a[0][3]=4

a[1][0]=5

a[1][1]=6

}

getch();

}

a[1][2]=7

a[1][3]=8

a[2][0]=9

a[2][1]=10

a[2][2]=11

a[2][3]=12

a[3][0]=13

a[3][1]=14

a[3][2]=15

a[3][3]=16

1 2 3 4

5 6 7 8

9 10 11 12

13 14 15 16

-----------

1 5 9 13

6 10 14

11 15

16

7

通过键盘给 3×4 的二维数组输入数据,然后分别按行和按列输出数组元素。

#define M 3

#define N 4

main()

{

int a

[N],i,j;

printf("Input integer number:n");

for(i=0;i

for(j=0;j

{

printf("a[%d][%d]=",i,j); /*提示输出元素位置*/

scanf("%d",&a[i][j]); /*为指定元素赋值*/

}

printf("nOut by row:n"); /*按行输出数组元素值*/

for(i=0;i

for(j=0;j

printf("%d ",a[i][j]);

printf("nOut by collum:n"); /*按列输出数组元素值*/

for(i=0;i

for(j=0;j

printf("%d ",a[j][i]);

getch();

}

【运行结果】

Input integer number:

a[0][0]=1

a[0][1]=2

a[0][2]=3

a[0][3]=4

a[1][0]=5

a[1][1]=6

a[1][2]=7

a[1][3]=8

a[2][0]=9

a[2][1]=10

a[2][2]=11

a[2][3]=12

Out by row:

1 2 3 4 5 6 7 8 9 10 11 12

Out by collum:

1 5 9 2 6 10 3 7 11 4 8 12

8

编写程序,将两个字符串连接起来,不要使用 strcat 函数。

#include"string.h"

main()

{

char str1[80],str2[80];

int i=0,l1;

printf("Input str1:");

gets(str1); /*若换成scanf("%s",str1);后面应该增加fflush(stdin);函数语句*/

printf("Input str2:");

gets(str2);

l1=strlen(str1); /*计算字符串 1 的长度*/

while(str2[i])

{

str1[l1+i]=str2[i];

i++;

}

str1[l1+i]='0'; /*字符串连接后,不能忘记追加一个字符串结束符*/

puts(str1); /*可换成语句:printf("%s",str1);*/

getch();

}

9

输入一行字符串,统计该字符串中字符对 ab 的个数。

#include"string.h"

main()

{

char str[80];

int i=0,count=0;

printf("Input string:");

gets(str);

while(str[i])

{

if(str[i]=='a' && str[i+1]=='b')

{

count++;

i+=2; /*如果在当前 i 的位置找到一对,则 i 越过 b 个字母*/

}

else i++; /*如果在当前 i 的位置未找到,则 i 指向下一个字母*/

}

printf("%d",count);

getch();

}

10

从键盘输入 10 个字符串,找出一个最长的字符串。

#include"string.h"

main()

{

char str[10][80]; /*设置二维字符数组,每一维可以记录最大79个字符的字符串*/

int i,len,k[10],n=0,max=0;

printf("Input 10 strings:n");

for(i=0;i<10;i++) /*向 str 数组输入 10 个字符串*/

{

printf("String %d=",i+1);

gets(str[i]);

len=strlen(str[i]); /*计算输入之字符串之长度*/

if(len>max){max=len;n=0;k[n]=i;} /*记录最长字符串的长度、个数、在数组位置(行)*/

else if(len==max)k[++n]=i; /*与已记录最大长度相同时,记录其在数组中位置(行)*/

}

if(n==0) /*此时说明只有一个最大长度字符串,是 str[k[0] */

{

printf("The longest string is String %d:",k[0]+1); /*输出其序号*/

puts(str[k[0]]);

}

else /*此时说明有不止一个最大长度字符串时需要处理的复合语句*/

{

printf("Not only one!n");

for(i=0;i<=n;i++) /*分别输出各序号、字符串*/

{

printf("These are String %d:",k[i]+1);

puts(str[k[i]]);

}

}

getch();

}

11

已知数组 a 中有 m 个按升序排列的元素,数组 b 中有 n 个按降序排列的元素,编程将 a 与 b 中

的所有元素按降序存入数组 c 中。

【提示】将 a 中的元素最大值与 b 中元素最大值相比,将最大值存入 c 数组中,然后调整 c 、a 或

b 元素指针(地址),依次重复前序工作,即可。

#define M 3

#define N 7

main()

{

int i=0,j=0,n=0,c[M+N],a[3]={10,27,543},b[7]={300,210,173,96,55,34,13};

int maxa,maxb;

do

{

maxa=a[M-i-1];maxb=b[j];

if(maxa>maxb)

{

c[n++]=maxa; /*将 a 中最大元素赋值给 c 数组当前元素,并调整 c 新元素位置*/

i++; /*调整 a 中元素位置*/

}

else

{

c[n++]=maxb; /*将 a 中最大元素赋值给 c 数组当前元素,并调整 c 新元素位置*/

j++; /*调整 b 中元素位置*/

}

printf("%d ",c[n-1]); /*打印输出 c 中新赋值元素数据*/

}while(n

getch();

}

第八章 指 针

-习题答案

1

★选择题。

1、若函数 f 定义如下:

void f(char *d,char *s);

{while(*d++=*s++);}

函数 f 的功能是( )。

A. 串比较 B. 串复制 C. 求串长 D. 串反向

2、有定义 int arr[10]={0,1,2,3,4,5,6,7,8,9}, *p, i=2; 若执行语句:

p=arr;

pritnf("%d",*(p+i));

输出结果为( )。

A. 0 B. 2 C. 3 D. 1

★3、有定义 int td[ ][3]={1,2,3,4,5,6}; 以下几种方法中,不能正确表示 i 行 j 列元素的是( )。

A. td[i][j] B. *(td[i]+j) C. *(*(td+i)+j) D. *(td+i-j) 本题不要求掌握

1、B 2、B 3、D

2

★写出下列程序的运行结果。

1、void main( )

{

int a,*p1,*p2;

a=10;

p1=&a;

p2=p1;

printf("The Value is %d||%d||%dn",a,*p1,*p2);

*p1=11;

printf("The Value is %d||%d||%dn",a,*p1,*p2);

}

2、若输入字符串 program 时,写出下列程序输出结果:

#include

void main( )

{

char str[80];

void prochar(char *st,char ch);

scanf("%s",str);

prochar(str,'r');

puts(str);

}

void prochar(char *str,char ch)

{

char *p;

for(p=str;*p!='0';p++)

if(*p==ch){*str=*p;(*str)++;str++;}

*str='0';

}

1、

The Value is 10||10||10

The Value is 11||11||11

2、ss

3

★填空题

1、下面程序完成从键盘输入两个字符串 a 和 b

组 c 中,填空完善该程序。

#include

再将 a 和 b 的对应位置字符中的较大者存放在数,

#include

void main( )

{

int k=0;

char a[80],b[80],c[80]={'0'},*p,*q;

p=a; q=b;

gets(a); gets(b);

while(_____①_____)

{

if(____②____)c[k]=*p;

else c[k]=*q;

p++; ______③______; k++;

} /*本题教材有误,少了此处的右大括号*/

if(*p!=0)strcat(c,p);

else strcat(c,q);

puts(c);

}

2、以下程序从键盘输入10个不相同的数到数组 a 中,再输入一个数到 x 中,在 a 中查找与 x 值

相同的元素所在的位置,填空完善该程序。。

#include

void main( )

{

int a[11],x,i;

printf("Input 10 integers:n");

for(i=1;___①___;i++)

scanf("%d",____②___);

printf("Input x:");

scanf("%d",&x);

*a=x; i=10;

while(x!=*(a+i))

_____③_____

if(_____④_____)printf("%6d's position is:%3dn",x,i);

else printf("%6d Not be found!n",x); /*bee应为be,教材有误,小case*/

}

1、① *p!='0'&&*q!='0' ② *p>*q ③ q++

2、① i<11 ② &a[i] ③ i- -; ④ i>0

4

编写函数 fun,函数的功能是,从字符串中删除指定的字符。同一字母的大、小写按不同字符处理。

例如,程序执行时输入字符串为 turbo c and borland c++,从键盘输入字符 n,则输出后变为 turbo c ad

borlad c++。如果输入的字符在字符串中不存在,则字符串照原样输出。

【提示】

#include"string.h"

main( )

{

char s[80],*pstr=s,chdel;

printf("Input source string:");

gets(pstr);

printf("Character you want delete:");

scanf("%c",&chdel);

fun(pstr,chdel);

printf("After deleted:");

puts(pstr);

getch();

}

int fun(char *s,char ch)

{

int i,j;

for(i=0,j=0;s[i];i++)

if(s[i]!=ch)

s[j++]=s[i];

s[j]=0;

}

5

编程将从键盘输入的每个单词的第一个字母转换为大写字母,输入时各单词必须用空格隔开,用“.”

结束输入。

【提示】。

【解法一】

#include"stdio.h"

int fun(char *p);

main( )

{

int i=0;

char ch,str[80];

printf("Input string end by.: n") ;

while((ch=getchar())!='.')

str[i++]=ch;

str[i++]='.'; str[i]='0';

fun(str);

puts(str);

getch();

}

int fun(char *p)

{

int flag=1;

while(*p!='0')

{

if(*p==' ')flag=1;

if(*p>='a'&&*p<='z'&&flag==1){*p-=32;flag=0;}

p++;

}

}

【解法二】

#include"stdio.h"

main( )

{

int flag=1;

char ch;

printf("Input string end by.: n") ;

do

{

ch=getchar( );

flag=fun(&ch, flag);

putchar(ch);

}while(ch!='.');

printf("n");

}

fun(char *c,int f)

{

if (*c==' ') return 1;

else

{

if(f&&*c<='z'&&*c>='a')*c-=32;

return 0;

}

}

6

从键盘输入 8 个数,用选择法按由大到小的顺序排列并输出,要求用指针实现。

【提示】

#include"stdio.h"

#include"string.h"

main( )

{

int data[80],i,*p1,*p2,temp;

p1=data;

printf("Input 8 numbers:");

for(i=0;i<8;i++)scanf("%d",p1++);

for(p1=data;p1

for(p2=p1+1;p2

if(*p1<*p2){temp=*p1;*p1=*p2;*p2=temp;}

for(i=0,p1=data;i<8;i++,p1++)printf("%6d",*p1);

getch();

}

7

从键盘输入一个字符串,编写一个函数,将此字符串中从第 m 个字符开始的 n 个字符复制成另一个

字符串。

#include"string.h"

#include"stdio.h"

main( )

{

char str1[80],str2[80];

int m,n,len;

printf("Input string:");

len=strlen(gets(str1));

printf("Input m,n:");

scanf("%d,%d",&m,&n);

if(m>len||n>len-m){printf("m,n error!");exit(0);}

else fun(str1,str2,m,n);

puts(str2);

getch();

}

fun(char *s,char *p,int x,int y)

{

int i;

for(i=0;i

}

8

输入 8 个整数,将其中最大的数与第一个数对换,最小的数与最后一个数对换。

main( )

{

int a[8],i,*p,max,min,t;

max=min=0;

p=a;

printf("Input 8 numbers:");

for(i=0;i<8;i++)scanf("%d",&a[i]);

for(i=1;i<8; i++)

if(*(p+i)<*(p+min))min=i;

if(min!=7)

{ t=*(p+7);

*(p+7)=*(p+min);

*(p+min)=t;

}

for(i=1;i<8; i++)

if(*(p+i)>*(p+max))max=i;

if(max!=0)

{ t=*(p+0);

*(p+0)=*(p+max);

*(p+max)=t;

}

for(p=a;p

getch();

}

9

输入 n 个整数排序并输出。要求将排序单独写成一个函数,n 个整数在主函数中输入,排序结果在主

函数中输出,用指针实现。【仍有问题】

#include"stdio.h"

sort(int **p, int n)

{

int *t,i,j;

for(i=0;i

{

for(j=i+1;j

{

if (**(p+i)>**(p+j))

{

t=*(p+i);

*(p+i)=*(p+j);

*(p+j)=t;

}

}

}

}

main( )

{

int i=0,n,**p;

printf("Please input count of numbers:n");

scanf("%d",&n);

for (i=0;i

{

printf("The %d number:",i+1);

scanf("%d",*(p+i));

}

printf("nThe result is :n");

sort(p,n);

for (i=0;i

{

printf("%d ",**(p+i));

}

getch();

}

10

输入一个字符串,内含数字和非数字字符,如 gs 423cd156 7896>? df12345,将其中连接的数字作

为一个整数,依次存放到数组 a 中,例如,423放入a[0],156放入a[1],…。统计其共有多少个整数,

并输出这些数。

未做

#include"string.h"

main()

{

char str[10][80]; /*设置二维字符数组,每一维可以记录最大79个字符的字符串*/

int i,len,k[10],n=0,max=0;

printf("Input 10 strings:n");

for(i=0;i<10;i++) /*向 str 数组输入 10 个字符串*/

{

printf("String %d=",i+1);

gets(str[i]);

len=strlen(str[i]); /*计算输入之字符串之长度*/

if(len>max){max=len;n=0;k[n]=i;} /*记录最长字符串的长度、个数、在数组位置(行)*/

else if(len==max)k[++n]=i; /*与已记录最大长度相同时,记录其在数组中位置(行)*/

}

if(n==0) /*此时说明只有一个最大长度字符串,是 str[k[0] */

{

printf("The longest string is String %d:",k[0]+1); /*输出其序号*/

puts(str[k[0]]);

}

else /*此时说明有不止一个最大长度字符串时需要处理的复合语句*/

{

printf("Not only one!n");

for(i=0;i<=n;i++) /*分别输出各序号、字符串*/

{

printf("These are String %d:",k[i]+1);

puts(str[k[i]]);

}

}

getch();

}


本文标签: 字符串 数组 元素 输出 输入