C语言编程练习11章Word格式.docx
- 文档编号:3494163
- 上传时间:2023-05-01
- 格式:DOCX
- 页数:18
- 大小:18.93KB
C语言编程练习11章Word格式.docx
《C语言编程练习11章Word格式.docx》由会员分享,可在线阅读,更多相关《C语言编程练习11章Word格式.docx(18页珍藏版)》请在冰点文库上搜索。
\0'
;
2.修改并测试练习1中的函数,使得可以在n个字符后,或第一个空格、制表符、换行符后停止读取输入,由上述情况中最先被满足的那个终止读取(不能用scanf()函数)。
ctype.h>
{
if(isspace(*(p+i)))break;
}
3.设计并测试一个函数,其功能是读取输入行里的第一个单词到数组,并丢掉该行中其他的字符。
一个单词的定义是一串字符,其中不含空格、制表符和换行符。
voidword(char*p);
gets(a);
word(a);
voidword(char*p)
intbegin,end;
for(begin=0;
isspace(*(p+begin));
begin++)continue;
for(end=begin;
!
isspace(*(p+end));
end++)continue;
*(p+end)='
for(;
*(p+begin)!
='
p++)
*p=*(p+begin);
*p='
4.设计并测试一个函数,其功能是搜索由函数的第一个参数指定的字符串,在其中查找由函数的第二个参数指定的字符的第一次出现的位置。
如果找到,返回指向这个字符的指针:
如果没有找到,返回空字符(这种方式和strchr()函数的功能一样)。
在一个使用循环语句为这个函数提供输入的完整程序中进行测试。
char*mystrchr(char*,char);
charstr[81];
charch;
char*p;
do
inputrangestring:
gets(str);
inputmatchchar:
ch=getchar();
p=mystrchr(str,ch);
if(p)
Find!
puts(p);
elseputs("
Can'
tfind!
inputanycharexceptqtogoon."
while(*str!
q'
Quit."
char*mystrchr(char*p,charch)
char*p_save=p;
if(*p=='
)returnNULL;
while
(1)
if(*p==ch)
returnp_save;
else
if(*++p=='
p_save=p;
5.编写一个函数is_witlun()。
它接受两个参数,一个是字符,另一个是字符串指针。
其功能是如果字符在字符串中,就返回一个非O值(真);
如果字符不在字符串中,就返回O值(假)。
intis_within(char*p,charch);
if(is_within(str,ch))puts("
while(ch!
intis_within(char*p,charch)
while(*p!
)
if(*p==ch)return1;
p++;
6.strncpy(sl,s2,n)函数从s2复制n个字符给sl,并在必要时截断s2或为其填充额外的空字符。
如果s2的长度等于或大于n,目标字符串就没有标志结束的空字符。
函数返回sl。
自己编写这个函数,并在一个使用循环语句为这个函数提供输入的完整程序中进行测试。
char*mystrncpy(char*p1,char*p2,intn);
charstr1[81];
charstr2[81];
inputstring1:
gets(str1);
inputstring2:
gets(str2);
inputthenumberofcopyingchar:
Aftercopying:
puts(mystrncpy(str1,str2,n));
while(*str1!
char*mystrncpy(char*p1,char*p2,intn)
char*p=p1;
while(*p1++!
)continue;
*--p1=*p2;
n--;
while(n>
0&
&
*p2!
*++p1=*++p2;
returnp;
7.编写一个函数string_in(),它接受两个字符串指针参数。
如果第二个字符串被包含在第一个字符串中,函数就返回被包含的字符开始的地址。
例如,string_in("
hats"
,"
at"
)返回hats中a的地址,则,函数返回空指针。
char*string_in(char*p1,char*p2);
charstr2[21];
inputmatchstring:
p=string_in(str1,str2);
char*string_in(char*p1,char*p2)
char*p1_save=p1,*p2_save=p2;
if(*p1=='
||*p2=='
if(*p1==*p2)
if(*++p2=='
)returnp1_save;
if(*++p1=='
p1_save=p1;
p2=p2_save;
8.编写一个函数,其功能是使输入字符串反序。
voidreverse(char*p);
inputastring:
reverse(str);
puts(str);
voidreverse(char*p)
inti,n;
chartemp;
for(n=0;
*(p+n)!
n++)continue;
for(i=0;
i<
n-i;
i++)
temp=p[i];
p[i]=p[n-i];
p[n-i]=temp;
9.编写一个函数。
其参数为一个字符串,函数删除字符串中的空格。
在一个可以循环读取的程序中进行测试,直到用户输入空行。
对于任何输入字符串,函数都应该适用并可以显示结果。
voiddelspace(char*p1);
delspace(str);
voiddelspace(char*p1)
char*p2;
while(*p1!
)
if(*p1=='
'
p2=p1;
while(*p2!
*p2=*(p2+1);
p2++;
p1--;
//抵消下面的p1++
p1++;
10.编写一个程序,读取输入,直到读入了10个字符串或遇到EOF,由二者中最先被满足的那个终止读取过程。
这个程序可以为用户提供一个有5个选项的菜单:
输出初始字符串列表、按ASCII顺序输出字符串、按长度递增顺序输出字符串、按字符串中第一个单词的长度输出字符串和退出。
菜单可以循环,直到用户输入退出请求。
当然,程序要能真正完成菜单中的各项功能。
string.h>
voidorigin_put(char**p,intn);
voidascii_put(char**p,intn);
voidlength_put(char**p,intn);
intfirst_word_length(char*p);
voidword_put(char**p,intn);
charstr[10][81];
char*p[10];
charcommand[10];
n=0;
inputnomorethan10stringsfinishedbyEOF(^Z):
if(gets(str[n])==NULL)break;
p[n]=str[n];
n++;
while(n<
10);
select:
a.putoriginally"
b.putinorderofascii"
c.putinorderofstring'
slength"
d.putinorderoffirstword'
e.inputstringsagain"
q.quit"
gets(command);
switch(command[0])
case'
a'
:
putoriginally:
origin_put(p,n);
break;
b'
putinorderofascii:
ascii_put(p,n);
c'
putinorderofstring'
slength:
length_put(p,n);
d'
putinorderoffirstword'
word_put(p,n);
e'
default:
while(command[0]!
);
voidorigin_put(char**p,intn)
i<
puts(p[i]);
voidascii_put(char**p,intn)
inti,j;
char*temp;
for(j=0;
j<
n-i-1;
j++)
if(strcmp(p[j],p[j+1])>
0)
temp=p[j];
p[j]=p[j+1];
p[j+1]=temp;
voidlength_put(char**p,intn)
if(strlen(p[j])>
strlen(p[j+1]))
intfirst_word_length(char*p)
inti=0;
for(;
isalpha(*p);
if(*p=='
)return0;
for(i=1;
isalpha(p[i]);
i++)continue;
returni;
voidword_put(char**p,intn)
if(first_word_length(p[j])>
first_word_length(p[j+1]))
11.编写一个程序。
功能是读取输入,直到遇到EOF,并报告单词数、大写字母数、小写字母数、标点符号数和数字字符数。
使用ctype.h系列的函数。
intword=0,upper=0,lower=0,punct=0,digit=0,begin=0;
while((ch=getchar())!
=EOF)
if(isalpha(ch))
if(begin==0)
word++;
begin=1;
elsebegin=0;
if(isupper(ch))upper++;
if(islower(ch))lower++;
if(ispunct(ch))punct++;
if(isdigit(ch))digit++;
printf("
word:
%d\nupper:
%d\nlower:
%d\npunct:
%d\ndigit:
%d\n"
word,upper,lower,punct,digit);
12.编写一个程序,按照相反的单词顺序显示命令行参数。
即,如果命令行参数是seeyoulater,程序的显示应该为lateryousee。
char*p,temp;
inti,n,length;
for(p=str,i=0,n=strlen(str);
n/2;
i++)//将整个字符串倒序
temp=*(p+n-1-i);
*(p+n-1-i)=*(p+i);
*(p+i)=temp;
p=str;
length=0;
do//将字符串中的每个单词倒序
if(isalpha(*p))
length++;
if(length>
1)
length/2;
temp=*(p-1-i);
*(p-1-i)=*(p-length+i);
*(p-length+i)=temp;
length=0;
}
while(*p++!
13.编写一个计算乘幂的基于命令行的程序。
第一个命令行参数为double类型数,作为幂的底数;
第二个参数为整数,作为幂的指数。
doublemypower(doublebase,intexp);
doublebase;
intexp;
inputbasenumberandexp:
%lf%d"
&
base,&
exp);
%g^%d=%g\n"
base,exp,mypower(base,exp));
doublemypower(doublebase,intexp)
doublepower=1;
if(exp<
0)
power=1/mypower(base,-exp);
elseif(exp>
while(exp-->
0)
power*=base;
elseif(base!
=0)
power=1;
power=1/base;
//0的0次幂应是一个无意义数
returnpower;
14.使用字符分类函数实现atoi()函数。
stdlib.h>
doublemyatof(char*p);
chara[30]={0};
while(a[0]!
puts
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 语言 编程 练习 11