实验九 数组(2)
班级______________姓名___________指导老师_____________成绩___________
第_____________机房___________号机器 时间:________________________
实验目的:1.进一步掌握一维数组、二维数组的使用。
2.熟练掌握字符数组的定义与使用。
3.掌握使用字符数组编写程序。
实验要求:每次实验前,学生要先预习实验内容,写出分析结果或程序,经实验指导老师检查后,上机运行、调试程序,得出最终正确结果。
实 验 内 容
一、读程序
要求:读懂程序,写出分析结果;上机运行程序,得到运行结果,比较之。
1.以下程序执行时输入 I am a student.<CR>,写出运行结果。
#include <stdio.h>
#include <string.h>
main()
{ char str[30 ] ;
scanf(“%s”,str);
printf(,str = %s \n”,str );
gets(str) ;
printf(,str = %s \n”,str );
}
分析结果
运行结果
2,
#include <string.h>
#include <stdio.h>
main()
{
char s1[50]=,I love,;
char s2[ ]=” China!”
printf(“%d,\n”,strlen(s2) );
strcat(s1,s2);
C语言程序设计上机指导与同步训练
·46·
·46·
printf(,%s \n”,s1);
}
分析结果
运行结果
3,
#include <stdio.h>
#include <string.h>
main()
{
char b[ 7 ] = {,54da12”} ;
int i,s=0 ;
for ( i =0 ; b [ i ] >=’ 0 ’ && b [ i ]<=’ 9 ’ ;i+ =2)
s = 10*s + b [ i ] – ‘ 0 ‘ ;
printf (,%d \n,,s ) ;
}
分析结果
运行结果
4.运行以下程序时,从键盘输入 ab<CR>
c<CR>
def<CR>
#include <stdio.h>
#include <string.h>
#define N 6
main( )
{
char c[N ] ;
int i=0;
for ( ; i < N ; c [ i ] = getchar(),i++) ;
for ( i = 0 ; i < N; i+ + ) putchar( c [ i ] );
}
分析结果
运行结果
5,
#include <stdio.h>
#include <string.h>
main()
{
int i= 0 ;
char a [ ] =,abm” ;
char b [ ] =,aqid”,c [10] ;
while ( a [ i ] != ‘\0’ && b [ i ] != ‘\0’ )
{
第一部分 C语言程序设计上机指导
·47·
·47·
if ( a [ i ] >= b [ i ] ) c [ i ] = a [ i ] - 32 ;
else c [ i ] = b [ i ] - 32 ;
+ + i ;
}
c [ i ] = ‘ \ 0 ‘ ;
puts ( c ) ;
}
分析结果
运行结果
6.当运行以下程序时,从键盘输入:teacher<CR>和student<CR>,写出运行结果。
#include <stdio.h>
#include < string.h>
main()
{
int i,j,m,n,i,max ;
int a[ 2 ][ 80 ] ;
for ( i=0 ; i < =1 ; i + +) gets( a[ i ]) ;
t = a [ 0 ][ 0 ] ;
for ( i=0 ; i < =1 ; i + +)
{ l = strlen ( );
for ( j = 0 ; j < =1 ; j + + )
if ( a [ i ][ j ] > t )
{ max = a [ i ][ j ] ; m = i ; n = j ;}
}
printf (,%d % d %d \n”,max,m,n );
}
分析结果
运行结果
二、完成程序
要求:根据要求,分析给出的上下语句,填写程序。
1.以下程序的功能是输出两个字符串中对应相等的字符,请在_______上填写正确
内容。
#include <stdio.h>
#include <string.h>
main( )
{ char x [ ]=,programming” ;
char y [ ] =,Fortran” ;
int i=0 ;
while( x[ i ] ! = ‘ \0 ’ && y [ i ] ! = ‘ \0 ’)
if ( x [ i ] == y [ i ] ) printf (,%c,,___________ ) ;
else ___________ ;
}
C语言程序设计上机指导与同步训练
·48·
·48·
2.下面程序的功能是将字符串a中所有的字符 c 删除,请在_______上填写正确内容。
#include <stdio.h>
#include <string.h>
main()
{
char s [80] ;
int i,j ;
gets(s) ;
for ( i = j = 0 ; s [ i ] ! = ‘ \ 0 ‘ ; i + +)
if ( s [ I ] ! = ‘c’ ) ________________________;
s [ j ] = ‘ \ 0 ’ ;
puts( s ) ;
}
3.以下程序的功能是在一个字符数组中查找一个指定的字符,若数组中含有该字符则输出该字符在数组中第一次出现的位置(下标界);否则输出-1;请在_______上填写正确内容。
#include <stdio.h>
#include <string.h>
main()
{
char t [50 ],c = ‘ a ‘ ;
int n,k,j ;
gets ( t ) ;
n = _______________;
for ( k = 0 ; k < n ; k + + )
if( ______________ ) { j = k ; break ; }
else j = -1 ;
printf (,% d,,j ) ;
}
4.从键盘输入,book <CR> computer<CR>music<CR>game<CR>,想找出最大字符串,请在_______上填写正确内容。
#include < stdio.h>
#include < string.h>
main()
{
char str [ 10 ],temp [10] = {,Control,};
int i ;
for (i=0 ;i < 4 ;i++)
{ gets ( str ) ;
if ( ________________) strcpy ( temp,str ) ;
}
puts(temp) ;
}
5.以下程序的功能是:求出每个字符串的长度,并输出字符串和长度。请在_______上填写正确内容。
第一部分 C语言程序设计上机指导
·49·
·49·
#include < stdio.h>
#include <string.h>
main()
{ char a [ 2 ] [ 10 ] = {,Delphi 7”,,Server”} ;
int i,j,len [ 2 ] ;
for ( i=0 ; ________ ; i + +)
{ for ( j = 0 ; j < 10 ; j + + )
if ( a [ i ][ j ] == ‘ \ 0 ‘ )
{ ____________;
break ;
}
printf (,% s,% d \n,,a [ i ],len [ i ] ) ;
}
}
三、调试程序
要求:分析下列程序是否正确,如有错,错在哪里?应如何改正?如正确,运行结果如何?上机调试之。
1,
main()
{ char a[ ] ;
int I,len=0 ;
a =,C Language Program” ;
for (i=0 ; a [ i ] != ‘ \0 ’ ;i+ +)
len + + ;
printf(,%s,“,a ) ;
}
运行结果,正确
错误所在,错误
应改为,
2,
main()
{ char a [ 10 ],b [ 10 ] ;
int i ;
for ( i = 0 ; i < 10 ; i + + )
scanf (“% c,,a [ i ] ) ;
b = a ;
for ( i =1 ; b [ i ] != ‘ \ 0 ‘ ; i + +)
printf(,% c,,b[i]) ;
}
C语言程序设计上机指导与同步训练
·50·
·50·
运行结果,正确
错误所在,错误
应改为,
3.下面程序的功能是:将字符数组a [6] ={ ‘a’,‘ b ’,‘ c ’,‘ d ’,‘ e ’,‘ f ’ } 变为 a [6]
={‘ f ’,‘a’,‘ b ’,‘ c ’,‘ d ’,‘ e ’,}。
main()
{ int i ;char t;
char a [ 6 ] = { ‘a’,‘ b ‘,‘ c ‘,‘ d ‘,‘ e ‘,‘ f ‘ } ;
t = a [5 ] ;
for (i=5 ; a [ i ] ! = ‘ \ 0 ‘ ; i - - )
a [ i ] = a [ i – 1 ] ;
a [ 0 ] =t;
for (i=0 ;i < =5 ;i++)
printf (“%c”,a [ i ] );
printf (,\n”) ;
}
运行结果,正确
错误所在,错误
应改为,
4.下面程序的功能是在三个字符串中找出最小的字符串。
#include <stdio.h>
#include < string.h>
main()
{ int i ;
char s [ 20 ],str [ 3 ][ 20 ] ;
for (i= 0 ; i < 3 ; i ++) gets ( str [ i ] ) ;
{ if ( str [ 0 ] < str [ i ] ) s = str [ 0 ] ;
else s = str [ 1] ;
if ( str [ 2 ] < s ) s = str [ 2 ] ;
}
printf(,\n” );
printf(,Min String is,%s”,s) ;
}
运行结果,正确
错误所在,错误
应改为,
第一部分 C语言程序设计上机指导
·51·
·51·
四、写程序
1.编写一个程序,从键盘输入一行字符,统计其中有多少个单词,单词之间用空格分隔,并输出单词的个数。
2.编写一个将一个字符串逆转的程序,如将a [ ] =,Computer,改为a [ ] =,retupmoC,。
3.编写一个将一个字符串中所有旧字符替换成新字符的程序。
4.编写一个程序统计某班3门课程的成绩,他们是 VB,Access,Photoshop。先输入
g4410g10995g1166数,g9994g2530g6365编号从小g2052大的g20046序g1393次输入g4410g10995成绩,最g2530统计每门课程g1852班的g5647成绩和g5191g3355成绩以g2462每个g4410g10995课程的g5647成绩和g5191g3355成绩。