您的位置:首页 > 编程语言

记录——《C Primer Plus (第五版)》第十一章编程练习第5-12题

2015-12-18 16:02 465 查看
5.编写一个函数is_within(),它接受两个参数,一个是字符,另一个是字符串指针。其功能是如果字符在字符串中,就返回一个非0值(真);如果字符不在字符串中,就返回0值(假)。在一个使用循环语句为这个函数提供输入的完整程序中进行测试。

# include <stdio.h>

int is_within(char ch, char * string);
void clear();//   清除每次输入的换行符
int main(void)
{
char ch;
char * string = "See you at the snack bar.";

printf("本程序是检测字母是否在字符串中,若想退出请输入小写字母 q\n");
printf("enter character:\n");
scanf("%c", &ch);
while(ch != 'q')
{
if(is_within(ch, string))
printf("子母 %c 在字符串中;\n", ch);
else
printf("子母 %c 不在字符串中;\n", ch);
printf("enter character:\n");
clear();
scanf("%c", &ch);
}

return 0;
}

int is_within(char ch, char * string)
{
int i = 1;
while(*string != ch && *string != '\0')
string++;
if('\0' == *string)
i = 0;
return i;
}

void clear()
{
char ch;
while((ch = getchar()) != '\n')
continue;
}


6.strncpy(s1, s2, n)函数从s2复制n个字符给s1,并在必要时截断s2或为其填充额外的空字符。如果s2的长度等于或大于n,目标字符串没有标志结束的空字符。函数返回s1。自己编写这个函数,并在一个使用循环语句为这个函数提供输入的完整程序中进行测试。

# include <stdio.h>

char *copystring(char *s1, char *s2, int n);
void clear();
int main(void)
{
char s1[11] = "Hell !";
char s2[31] = "sunddddddddddays.";
int n;

for(int i = 0; i < 3; i++)
{
printf("请输入你要复制的长度:\n");
scanf("%d", &n);
clear();
copystring(s1,s2,n);
puts(s1);

}
return 0;
}

char *copystring(char *s1, char *s2, int n)
{
for(int i = 0; i < n; i++)
{
if('\0' == s2[i])
{
s1[i] = '\0';
break;
}
s1[i] = s2[i];
}
if(n == i)
{
s1[i] = '\0';
}

return s1;
}

void clear()
{
char ch;
while((ch = getchar()) != '\n')
continue;
}


7.编写一个函数string_in(),它接受两个字符串指针参数。如果第二个字符串被包含在第一个字符串中,函数就返回被包含的字符串开始的地址。例如,string_in(“hats”, “at”)返回hats中a的地址,否则,函数返回空指针。在一个使用循环语句为这个函数提供输入的完整程序中进行测试。

# include <stdio.h>

char *instring(char * s1, char * s2);
int main(void)
{
char * p;
char *s1 ="headfhloafkjafd";
char *s2 = "ea";   //有相同,instring()函数返回字母e的地址

p = instring(s1, s2);
printf("%c\n", *p);
return 0;
}

char *instring(char * s1, char * s2)
{
int is = 1;  //判断循环是否继续
int i = 0;   //表示s2中第一个字母是否与s1中有相同的字母
char *p;//p为s2字符串的第一个字母

while(is)
{

if(0 == i)   //i = 0时,s1中还没找到相同于s2字符串的首字符
{
if(*s1 == *s2)//
{
i=1;
p = s2;
s1++;
s2++;
}
else
s1++;
}
if(1 == i)  //i= 1时,匹配s2剩下的字母,一旦不同,令i = 0
{
if(*s1 == *s2)
{
s1++;
s2++;
}
else
{
if('\0' == *s2)//  当s2值为0时,s1中就有与s2字符串相同的部分
return p;
else
{
i = 0;
s1++;
s2 = p;
}

}
}
if('\0' == *s1)
is = 0;
}
return NULL;
}


8.编写一个函数,其功能是使输入字符串反序。在一个使用循环语句为这个函数提供输入的完整程序中进行测试。

# include <stdio.h>
# include <string.h>

void reverse(char * target);
int main(void)
{
char target[30];
for(int i = 0; i < 3; i++)
{
gets(target);
reverse(target);
puts(target);
}

return 0;
}

void reverse(char * target)
{
int len;
char t;
len = strlen(target);
for(int i = 0; i < len/2; i++)
{
t = target[i];
target[i] = target[len-i-1];
target[len-i-1] = t;
}
}


9.编写一个函数,其参数为一个字符串,函数删除字符串中的空格。在一个可以循环读取的程序中进行测试,直到用户输入空行。对于任何输入字符串,函数都应该适用并可以显示结果。

# include <stdio.h>

void deletespace(char * target);
int main(void)
{
char target[31];

while(*target)
{
gets(target);
deletespace(target);
puts(target);
}

return 0;
}

void deletespace(char * target)
{
char * ch;
while(*target)
{
if(' ' == *target)
{
while(*target)
{
ch = target;
target++;
* ch = *target;
}
}
else
target++;
}
}


10,编写一个程序,读取输入,直到读入了10个字符串或遇到EOF,由二者中最先被满足的那个终止读取过程。这个程序可以为用户提供一个有5个选项的菜单:输出字符串列表、按ASCII顺序输出字符串、长度递增顺序输出字符串、按字符串中第一个单词的长度输出字符串、退出。菜单可以循环直到用户输入退出请求。当然,程序要能真正完成菜单中的各项功能。

# include <stdio.h>
# include <string.h>

void clear();
void show_string(char *target[10], int n);
void ascll_string(char *target[10], int n);
void len_string(char *target[10],int n);
int len_word_first(char *target);
void strlen_firstword(char *target[10], int n);
int main(void)
{
char target[10][50];
char *ptr_str[10];//初始字符串
char *ptr_ascll[10];//按ASCLL码顺序的字符串
char *ptr_len[10];//按长度递增顺序的字符串
char *ptr_firstword[10];//按字符串中第一个单词的长度输出的字符串
char ch;
int i = 0;

printf("请最多输入10个字符串.\n");
while(i != 10 && gets(target[i]) != NULL && target[i][0] != '\0')
{
ptr_str[i] = target[i];
ptr_ascll[i] = target[i];
ptr_len[i] = target[i];
ptr_firstword[i] = target[i];
i++;
}

printf("----------字符串的操作-------------------\n");
printf("  s 输出初始字符串\n");
printf("  a 按ASCLL顺序输出字符串\n");
printf("  l 按长度递增顺序输出字符串\n");
printf("  f 按字符串中第一个单词的长度输出字符串\n");
printf("  e 退出\n");
printf("-----------------------------------------\n");

while((ch = getchar()) != 'e')
{
clear();
switch(ch)
{
case 's':
show_string(ptr_str, i);
break;
case 'a':
ascll_string(ptr_ascll,i);
show_string(ptr_ascll, i);
break;
case 'l':
len_string(ptr_len, i);
show_string(ptr_len, i);
break;
case 'f':
strlen_firstword(ptr_firstword, i);
//      printf("%d\n", len_word_first(ptr_firstword[1]));
show_string(ptr_firstword, i);
break;
case 'e':
return 0;
default :
printf("输入错误,请重新输入!");
}
printf("----------字符串的操作-------------------\n");
printf("  s 输出初始字符串\n");
printf("  a 按ASCLL顺序输出字符串\n");
printf("  l 按长度递增顺序输出字符串\n");
printf("  f 按字符串中第一个单词的长度输出字符串\n");
printf("  e 退出\n");
printf("-----------------------------------------\n");
}

return 0;
}

void clear()
{
char ch;
while((ch = getchar()) != '\n')
continue;
}

void show_string(char *target[10], int n)
{
for(int i = 0; i < n; i++)
{
puts(target[i]);
}
}

void ascll_string(char *target[10], int n)
{
char *str;
for(int i = 0; i < n-1; i++)
{
for(int j = 0; j < n-1; j++)
{
if( strcmp(target[j], target[j+1]) > 0 )
{
str = target[j];
target[j] = target[j+1];
target[j+1] = str;
}
}
}
}

void len_string(char *target[10],int n)
{
char *str;
for(int i = 0; i < n-1; i++)
{
for(int j = 0; j < n-1; j++)
{
if( strlen(target[j]) > strlen(target[j+1]) )
{
str = target[j];
target[j] = target[j+1];
target[j+1] = str;
}
}
}
}

int len_word_first(char *target)
{
int i = 0;
char ch = *target;
while(ch < 65 || ch > 122 || (ch < 97 && ch > 90))
{
ch = *target++;
}
while( (ch > 65 && ch < 97) || (ch > 90 && ch < 122) )
{
i++;
ch = *target++;
}
return i;
}

void strlen_firstword(char *target[10], int n)
{
char *str;
for(int i = 0; i < n-1; i++)
{
for(int j = 0; j < n-1; j++)
{
if( len_word_first(target[j]) > len_word_first(target[j+1]) )
{
str = target[j];
target[j] = target[j+1];
target[j+1] = str;
}
}
}
}


11.编写一个程序。功能是读取输入,直到遇到EOF,并报告单词数、大写字母数、小写字母数、标点符号数和数字字符数。使用ctype.h系列函数。

# include <stdio.h>
# include <ctype.h>

int main(void)
{
int words = 0;//单词数
int uppercase = 0;//大写字母
int lowercase = 0;//小写字母
int punctuation = 0;//标点符号数
int digit = 0;//数字字符数
int i = 0;//  判断单词
char ch1, ch2;

while((ch1 = getchar()) != '\n')
{
i = 0;
if(islower(ch1))
{
lowercase++;
i = 1;
}
else if(isupper(ch1))
{
uppercase++;
i = 1;
}
else if(ispunct(ch1))
{
punctuation++;
}
else if(isdigit(ch1))
{
digit++;
}
if(1 == i)
{
ch2 = ch1;
}
if( (ch2 >= 65 && ch2 <= 97) || (ch2 >= 90 && ch2 <= 122) )
{
if(ch1 < 65 || ch1 > 122 || (ch1 < 97 && ch1 > 90) )
{
words++;
ch2 = ch1;
}
}
}
if( (ch2 >= 65 && ch2 <= 97) || (ch2 >= 90 && ch2 <= 122))
words++;
printf("%d %d %d %d %d\n", words, uppercase, lowercase, punctuation, digit);
return 0;
}


12.编写一个程序,按照相反的单词顺序显示命令行参数。即,如果命令行参数是see you later,程序的显示应该为later you see。

# include <stdio.h>
# include <stdlib.h>
# include <string.h>

int main(int argc, char *argv[])
{
char *ps;

for(int i = 0; i < argc / 2; i++)
{
ps = argv[i];
argv[i] = argv[argc-1-i];
argv[argc-1-i] = ps;
}

for(int j = 0; j < argc; j++)
{
puts(argv[j] );
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: