函数String(n,"str")的功能是()。
第1题:
请编写一个函数int compare(char *s,char *t)), 该函数的功能是对两个字符串进行比较。当s所指字符串和t所指字符串相等时,返回值为0;当s所指字符串大于是t指字符串时,返回值大于0;当s所指字符串小于t所指字符串时,返回值小于0。
注意:部分源程序已存在文件PROC8.cpp中。
文件PROC8.cpp的内容如下:
//PROC8.cpp
include <iostream>
include <string>
using namespace std;
int compare(char *s,char *t)
{
//* * * * * * * * *
}
int main ()
{
char str1[100],str2[100];
int result;
cout<<"Input the first string\n";
cin>>str1;
cout<<"Input the second string\n";
cin>>str2;
result=compare(str1,str2);
if (result==0)
cout<<"string1=string2 ! \n";
else if (result>0)
cout<<"string1>string2 ! \n";
else
cout<<"string1<string2 ! \n";
return 0;
}
第2题:
请补充函数fun(),该函数的功能是:依次取出字符串中所有的小写字母以形成新的字符串,并取代原字符串。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun()的横线上填入所编写的若干表达式或语句。
试题程序:
include<stdio.h>
include<conio.h>
void fun(char *s)
{
int i=0;
char *p=s;
while(【 】)
{
if (*p>='a'&&*p<='z')
{
s[i]=*p;
【 】;
}
p++;
}
s[i]=【 】;
}
main()
{
char str[80];
clrscr();
printf("\nEnter a string:");
gets(str);
printf("\n\nThe string is:\%s\n",str);
fun(str);
printf("\n\nThe string of changing
is:\%s\n",str);
}
第3题:
请编写一个函数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) ;
}
第4题:
请补充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);
}
第5题:
请补充函数fun(),该函数的功能是:把字符下标为非素数的字符从字符串sb中删除,把字符下标为素数的字符重新保存在字符串e口中。字符串sb从键盘输入,其长度作为参数传入函数fun()。
例如,输入“abcdefghijkl”,输出“cdfhl”。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的横线上填入所编写的若干表达式或语句。
试题程序:
include <stdio.h>
define N 80
void fun(char s[],int n)
{
int i, j, k, flag;
【 】;
for(i=0; i<n; i++)
{
if (i>1)
s [k++] =s [i];
flag=I;
for(【 】; j<i&&flag; j++)
if (i%j==0)
{
flag=0;
【 】
}
}
s [k]='\0';
}
main()
{
int i=0, strlen=0;
char str[N];
clrscr ();
printf("\n Input a string:\n");
gets (str);
while (str [i] !='\0')
{
strlen++;
i++;
}
fun (str, strlen);
printf("\n*** display string ***\n");
puts (str);
}
第6题:
请补充main函数,该函数的功能是:从一个字符串中截取前面若干个给定长度的子字符串。其中,str1指向原字符串,截取后的字符存放在str2所指的字符数组中,n中存放需截取的字符个数。
例如:当str1=“cdefghij”,然后输入4,则str2=“cdef”。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun()的横线上填入所编写的若干表达式或语句。
试题程序:
include<stdio.h>
include<conio.h>
define LEN 80
main()
{
char str1[LEN),str2[LEN);
int n,i;
clrscr();
printf(“Enter the string:\n”);
gets(str1);
printf“Enter the position of the string
deleted:”);
scanf(【 】);
for(i=0;i<n;i++)
【 】
str2[i]=‘\0’;
printf(“The new string is:%s\n”,【 】);
}
第7题:
请补充函数proc,该函数的功能是:把从主函数中输入的由数字字符组成的字符串转换成一个无符号长整数,并且逆序输出。结果由函数返回。例如,输入:1234567,结果输出:7654321。 注意:部分源程序给出如下。 请勿改动main函数和其他函数中的任何内容,仅在函数proc的横线上填人所编写的若干表达式或语句。 试题程序: include<stdlib.h> include<conio.h> include stdio.h> include<string.h> unsigned long proc(char*str) { unsigned long t=0: int k: int i=0; i=str|en(str); for(【1】 ;i>=0;i--) { k= 【2】 ; t= 【3】 ; } return t; } void main { char str[8]; system("CLS"); printf("Enter a string made up of0to 9 digital character:\n"); gets(str); printf("The string,is:%s\n",str); if(strlen(str)>8) printf("The string is too tong!"); else printf("The result:%1u\n", proc(str)); }
【1】--i【2】str[i]-0 【3】t*10+k
【解析】由函数proc可知,变量i首先被赋值为字符串str的长度,for循环中第一个条件为字符串str最后一个字符的下标,因此【1】填“-i”;要将字符串转化为长整型,需要将每一个字符转化为数字,因此【2】填“str[i]-0”;然后将各个数字转化为一个无符号长整数,因此【3】填“t*10+k”。
第8题:
●试题三
请补充函数fun(),该函数的功能是:只保留字符串中的大写字母,删除其他字符,结果仍保存在原来的字符串中,由全局变量m对删除后字符串的长度进行保存。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun()的横线上填入所编写的若干表达式或语句。
试题程序:
#include<stdio.h>
#include<conio.h>
int m;
void fun(char *s)
{
int i=0,j=0;
char *p=s;
while(*(p+i))
{
if(*(p+i)>=′A′&&*(p+i)<=′Z′)
{
(1) ;
}
(2) ;
}
s[j]=′\0′;
(3) ;
}
main()
{
char str[80];
clrscr();
printf("\nEnter a string:");
gets(str);
printf("\n\nThe string is:\%s\n",str);
fun(str);
printf("\n\nThe string of changing
is:\%s\n",str);
printf("\n\nThe length of changed string
is:\%d\n",m);
}
●试题三
【答案】(1)s[j++]=*(p+i)(2)i++(3)m=j
【解析】填空1:将大写字母存于原字符串中,数组下标j自加1,为下一次存放做准备。填空2:为了判断字符串中的下一个字符是否为大写字母,变量i自加1。填空3:最后将大写字母的个数,即删除后字符串的长度赋给全局变量m。
第9题:
请补充函数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);
}
第10题:
请补充函数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);
}