用“起泡法”对输入的10个字符排序后按从小到大的次序输出。
#define N 10
char str[N];
main()
{ int i,flag;
for(flag=1;flag==1;)
{ scanf("%s",str);
flag=0;
printf("\n");
}
sort(___(4)___);
for(i=0;i<N;I++)
printf("%c",str[i]);
printf("\n");
}
sort(char str[N])
{ int i,j;
char t;
for(j=1;j<N;J++)
for(i=0;(i<N-J)&&(STR[I]!='\0');I++)
if(str[i]>str[i+1])
{ t=str[i];
____(5)____;
____(6)____;
}
}
第1题:
请补充main函数,该函数的功能是:从键盘输入一个字符串及一个指定字符,然后把这个字符及其后面的所有字符全部删除。结果仍然保存在原串中。 例如,输入“abcdefg”,指定字符为“d”,则输出“abe”。 注意:部分源程序给出如下。 请勿改动main函数和其他函数中的任何内容,仅在main函数的横线上填入所编写的若干表达式或语句。 试题程序: include<stdlib.h> include<stdio.h> define M 80 void main { int i=0; char str[M]; char ch; system("CLS"); printf("\n Input a strin9:\n"); gets(str); printf("\n Input a charator;\n"); scanf("%c",&ch); while(str[i]!=\0) { if(str[i]==ch) 【1】 【2】 ; } str[i]=【3】 ; printf("\n***display string***\n"); puts(str); }
【1】break;【2】i++【3】\0
【解析】题目要求删除指定字符后的所有字符,将字符串中的字符从第一个开始与指定字符相比较,直到找到第一个与指定字符相同的字符结束。因此,【1】处填“break;”;【2】处填“i++”;为新的字符串添加结束符,因此,【3】处填“\O”。
第2题:
请补充函数fun(),该函数的功能是:把ASCII码为奇数的字符从字符串str中删除,结果仍然保存在字符串str中。字符串str从键盘输入,其长度作为参数传入函数fun()。
例如,输入“abcdef”,输出“bdf”。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun()的横线上填入所编写的若干表达式或语句。
试题程序:
include <stdio.h>
define N 80
void fun(char s[],int n)
{
int i, j;
j=0;
for(i=0;【 】;i++)
{
if(【 】)
s [j++]-s [i];
}
【 】;
}
main ( )
{
int i=0, strlen=0;
char str [N];
clrscr ();
printf ("\nInput a string: \n");
gets (str);
while (str [i] !='\0')
{
strlen++;
i++;
}
fun(str, strlen);
printf("\n*** display string ***\n");
puts (str);
}
第3题:
( 12 ) 有以下程序
#include <stdio.h>
#include <string.h>
void fun ( char *str )
{ char temp;int n,i;
n=strlen ( str ) ;
temp=str[n-1];
for ( i=n-1;i>0;i-- ) str[i]=str[i-1];
str[0]=temp;
}
main ()
{ char s[50];
scanf ( " %s " ,s ) ; fun ( s ) ; printf ( " %s\n " ,s ) ;}
程序运行后输入: abcdef< 回车 > ,则输出结果是 【 12 】 。
第4题:
程序中头文件type1.h的内容是( )。 #define N 5 #define M1 N*3 程序如下: #define "type1.h" #define M2 N*2 main() { int i; i=M1+M2; printf("%d\n",i); } 程序编译后运行的输出结果是( )。
A.10
B.20
C.25
D.30
第5题:
请补充函数fun(),该函数的功能是:返回字符数组中指定子符的个数,指定字符从键盘输入。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun()的横线上填入所编写的若干表达式或语句。
试题程序:
include <stdio.h>
define N 80
int fun (char s[],char ch)
{
int i=0, n=0;
while(【 】)
{
if(【 】)
n++;
i++;
}
【 】;
}
main ( )
{
int n;
char str[N], ch;
clrscr ();
printf ("\nInput a string: \n");
gets (str);
printf ("\nInput a charactor: \n" ;
scanf ("%c", &ch);
n=fun (str, ch);
printf("\nnumber of %c:%d", ch, n);
}
第6题:
阅读下列程序说明和C代码,把应填入其中n处的字句写在答卷的对应栏内。
【说明】
程序利用选择排序算法对数组a中的N个整数按照从小到大的顺序排列,并将排序结果显示出来。
【程序】
define N 10
main()
{
void (1);
int i,a[N];
for(i=0;i<10,i++) /*输入*/
scanf(“%d”,&a[i]);
(2);
for(i=0;i<N,i++) /*输出*/
printf(“%3d”,a[i]);
}
void selectSon(int x[],int n)
{
int i,j,k,t;
for(int i=0; (3);i++)
{
k=i;
for(j=i+1;j<n;j++)
if (4) k=j;
if (5)
{t=x[i];x[i]=x[k];x[k] =t;}
}
}
第7题:
请补充main函数,该函数的功能是:把字符串str中的字符向前移动一位,原来的第一个字符移动到字符串尾,结果仍然保存在原字符串中。
例如,输入“how do you do?”,则结果输出“ow do you do?h”。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在 main函数的横线上填入所编写的若干表达式或语句。
试题程序:
include <stdio. h>
define N 80
main()
{
char str[N], ch;
int i;
clrscr ();
printf("\n Input a string:In");
gets (str);
printf("\n*** original string ***In");
puts (str);
ch=str [0];
for (i=0;【 】; i++)
str [i]=str [i+1];
【 】;
printf("\n *** new string ***\n");
puts (str);
}
第8题:
请编写一个函数void fun(cbara [], charb [], int n),其功能是:删除一个字符申中指定下标的字符。其中,a指向原字符串,删除后的字符串存放在b所指的数组中,n中存放指定的下标。
例如,输入一个字符串world,然后输入3,则调用该函数后的结果为word。
注意:部分源程序给出如下。
请勿改动主函数main 和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
试题程序:
include <stdio.h>
include <conio.h>
define LEN 20
void fun (char a[], char b [], int n)
{
}
main ()
{
char str1 [LEN], str2 [LEN];
int n ;
clrscr ();
printf ("Enter the string : \n") ;
gets (str1) ;
printf ("Enter the position of the string
deleted: ");
scanf ("%d", &n) ;
fun (str1, str2, n) ;
printf ("The new string is : %s \n",
str2) ;
}
第9题:
请补充函数fun(),该函数的功能是:把字符串str中的字符按字符的ASCⅡ码降序排列,处理后的字符串仍然保存在原串中,字符串及其长度作为函数参数传入。
例如,如果输入“cdefgh”,则输出为“hgfedc”。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun()的横线上填入所编写的若干表达式或语句。
试题程序:
include <stdio.h>
define N 80
void fun (char s [], int n)
{
int i, j;
char ch;
for (i=0; i<n; i++)
for(j=【 】;j<n;j++)
if (s[i]<s [j])
{
ch=s [j];
【 】;
s [i] =ch;
}
main ( )
{
int i=0, strlen=0;
char str [N];
clrscr ();
printf ("\nInput a string: \n");
gets (str);
while (str [i] !=' \0')
{
strlen++;
i++;
}
fun (str, strlen);
printf ("\n***display string ***\n");
puts (str);
}
第10题:
下列给定程序中,函数fun()的功能是:从N个字符串中找出最长的那个中,并将其地址作为函数值返回。各字符串在主函数中输入,并放入一个字符串数组中。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
include <string.h>
include <stdio.h>
define N 5
define M 81
/*************found**************/
fun(char (*sq) [N])
{int i; char *sp;
sp=sq[0];
for{i=0;i<N;i++)
if(strlen(sp)<strlen(sq[i]))
sp=sq[i];
/*************found**************/
return sq;
}
main ()
{char str[N][M], *longest; int i;
printf("Enter %d lines:\n ",N);
for(i=0;i<N;i++) gets(str[i]);
printf("\nThe %d string :\n ",N);
for(i=0;i<N;i++) puts(str[i]);
longest=fun(str);
printf("\nThe longest string :\n ");
puts(longest);
}