《C Primer Plus(第五版)中文版》第11章第1至15题

/*
 ============================================================================
1.设计并测试一个函数,可以从输入读取n个字符(包括空格、制表符和换行符),把结果存储在
一个数组中,这个数组的地址通过参数来传递。
 ============================================================================
 */

#include <stdio.h>
#include <string.h>
char *pr(int n, char s[n]);
int main(void)
{
	char s[5];
	char *str = pr(5,s);
	for(int i = 0;i<5;i++){
		putchar(str[i]);
	}
    return 0;
}
char *pr(int n,char s[n]){
	char a;
	int i = 0;
	printf("请输入%d个字符:",n);
	while((a = getchar()) != '\0' && i < n){
		s[i] = a;
		i++;
	}
	return s;
}


/*
 ============================================================================
2.修改练习1,使得可以在n个字符后,或第一个空格、制表符,换行符后停止读取输入,由上述情况
最先被满足的那个终止读取,不能使用scanf()函数。
 ============================================================================
 */

#include <stdio.h>
#include <string.h>
char *pr(int n, char s[n]);
int main(void)
{
	char s[5];
	char *str = pr(5,s);
	for(int i = 0;i<5;i++){
		putchar(str[i]);
	}
    return 0;
}
char *pr(int n,char s[n]){
	char a;
	int i = 0;
	printf("请输入%d个字符:",n);
	a = getchar();
	while(a != '\0' && a != ' ' && a != '\t' && a != '\n' && i < n){
		s[i] = a;
		i++;
		a = getchar();
	}
	return s;
}

/*
 ============================================================================
3.设计并测试一个函数,其功能是读取输入行里的第一个单词到数组,并丢掉该行中其他
字符,一个单词的定义是一串字符,基中不含空格,制表及换行符。
 ============================================================================
 */

#include <stdio.h>
#include <string.h>
char *toword(int n, char * s);
int main(void)
{
	char s[20];
	toword(20,s);
	printf("这句话中第一个单词为:");
	for(int i = 0;i<strlen(s);i++){
		putchar(s[i]);
	}
    return 0;
}
char *toword(int n,char * s){
	char a;
	int i = 0;
	printf("请输入字符串:");
	a = getchar();
	//丢掉之前多余空格,换行,制表等符号
	while(a == '\n' || a == '\t' || a== ' '){
		putchar(a);
		a = getchar();
	}
	//获取第一个单词,并写入到数组中
	while(a != '\n' && a != '\t' && a != ' ' && i < n){
		*s = a;
		s++;
		i++;
		a = getchar();
	}
	*s = '\0';
	return s;
}

/*
 ============================================================================
4.设计并测试一个函数,其功能是搜索由函数的第一个参数指定的字符串,在其中查找由函数的第二
个参数指定的字符的第一次出现的位置。如果找到,返回指向这个字符的指针,如果没有找到,返回
空字符(这种方式和str()函数功能一样,在一个使用循环的语句中变回这个函数提供输入完整程序
中进行测试
 ============================================================================
 */

#include <stdio.h>
#include <string.h>
char * strfind(char *s1,char ch);
int main(void)
{
	char s1[20];
	char *s;
	char ch;
	ch = 'a';
	while(ch != 'q'){
		printf("请输入一个字符串\n");
		gets(s1);
		printf("请输入将要查找的字符:\n");
		ch = getchar();
		s = strfind(s1,ch);
		if(s == NULL){
			printf("没有找到你要查找的字符\n");
		}else{
			printf("找到了%c \n",*s);
		}
		while((temp = getchar()) != '\n'){
		                    putchar(temp);
		}
	}
    return 0;
}
char * strfind(char *s1,char ch){
	int n = strlen(s1);
	for(int i = 0;i<n;i++){
		if(ch == s1[i]) return &s1[i];
	}
	return NULL;
}

/*
 =======================================================================================
5.编写一个函数is_within()
 =======================================================================================
 */

#include <stdio.h>
#include <string.h>
int iswithin(char *s1,char ch);
int main(void)
{
	char s1[20];
	int a;
	char ch;
	ch = 'a';
	char temp;
	do{
		printf("请输入一个字符串\n");
		gets(s1);
		printf("请输入将要查找的字符:\n");
		ch = getchar();
		a = iswithin(s1,ch);
		if(a == 0){
			printf("没有找到你要查找的字符\n");
		}else{
			printf("找到了\n");
		}
		while((temp = getchar()) != '\n'){
					putchar(temp);
				}
	}while(ch != 'q');
    return 0;
}

int iswithin(char *s1,char ch){
	int n = strlen(s1);
	for(int i = 0;i<n;i++){
		if(ch == s1[i]) return i+1;
	}
	return 0;
}


/*
 ============================================================================
6.strcpy(s1,s2,n)函数从s2复制n个字符给s1,并在必要时截断s2或是为其真充必要的字符。如果
s2长度大于或者n,目标字符串就没有结束的空字符,函数返回s1,自己编写这个函数,并在一个循环
中测试这个函数。
 ============================================================================
 */

#include <stdio.h>
#include <string.h>
char * mystrcpy(char *s1,char *s2,int n);
int main(void)
{
	char ch,temp;
	char s1[20];
	char s2[20];
	int n = 0;
	ch = 'y';
	while(ch != 'q'){
		printf("请输入s2字符串的内容:\n");
		gets(s2);
		printf("请输入将要复制的字符串长度:\n");
		scanf("%d",&n);
		mystrcpy(s1,s2,n);
		printf("s1:%s\n",s1);
		while((temp = getchar()) != '\n'){
					putchar(temp);
				}
		printf("退出请按q,继续请按y:");
		ch =getchar();
		while((temp = getchar()) != '\n'){
			putchar(temp);
		}
	};
    return 0;
}

char * mystrcpy(char *s1,char *s2,int n){
	int x = strlen(s2);
	if(x < n){
		for(int i = 0;i<x;i++){
			s1[i] = s2[i];
		}
		s1[x] = '\0';
	}else{
		for(int i=0;i<n;i++){
			s1[i] = s2[i];
		}
		s1[n] = '\0';
	}
	return s1;
}

/*
 ============================================================================
7.编写一个函数string_in(),它接受两个字符串指针参数,如果第二个字符串被包含在第一个字
符串中,函数就返回被包含的字符串的开始的地址,例如string_in("hats","at")返回hats中a的
地址,否则返回空指针,在一个使用循环的程序进行测试这个函数。
 ============================================================================
 */

#include <stdio.h>
#include <string.h>
void outtemp(void); //丢弃缓冲区中的多余的输入
char * string_in(char *s1,char *s2); //查找字符串s2在s1中的位置,如果没有找到则返回空指针
char * string_inchar(char *s1,char ch); //查找一个字符是否在一个字符串中,如果在则返回这个字符所在位置地址,否则返回NULL
int main(void)
{
	char *s1 = "abadsadsdfs";
	char temp[20];
	char *s2 = temp;
	char ch = 'y';
	while(ch != 'q'){
		puts("请输入将要查找的字符串:");
		gets(s2);
		if(string_in(s1,s2)){
			printf("找到了\n");
		}else{
			printf("没有找到\n");
		}
		printf("继续请按y,退出请按q:\n");
		ch = getchar();
		outtemp();
	}
    return 0;
}

void outtemp(void){
	char temp;
	while((temp = getchar()) != '\n')
		putchar(temp);
}
char * string_in(char *s1,char *s2){
	char * t = s1;
	if(s1 == NULL || s2 == NULL) return NULL;
	int n_s1 = strlen(s1);
	int n_s2 = strlen(s2);
	int k = 0;
	if(n_s1 < n_s2) return NULL;
	while((t = string_inchar(s1,s2[0])) != NULL){
		k = 0;
		if(strlen(t) < n_s2){
			return NULL;
		}else{
			for(int i = 0;i<n_s2;i++){
				if(t[i] == s2[i]){
					k++;
				}else{
					break;
				}
			}
			if(k == n_s2) return t;
		}
        s1 = t + 1;
	}
	return NULL;
}
char * string_inchar(char *s1,char ch){
	for(int i = 0;i < strlen(s1);i++){
		if(ch == s1[i]) return &s1[i];
	}
	return NULL;
}


/*
 ============================================================================
8.编写一个函数,其功能使输入的字符串反序,在一个循环语句为这个函数提供完整的测试。
 ============================================================================
 */

#include <stdio.h>
#include <string.h>
void outtemp(void); //丢弃缓冲区中的多余的输入
char * str_back(char *s); //将字符串s反序处理
int main(void)
{
	char temp[20];
	char * ptemp = temp;
	char ch = 'y';
	while(ch != 'q'){
		puts("请输入一个字符串(不要超过20个字符):");
		gets(ptemp);
		puts("逆序后结果:");
		puts(str_back(ptemp));
		puts("继续测试请按y,退出请按q:");
		ch = getchar();
		outtemp();
	}
    return 0;
}

void outtemp(void){
	char temp;
	while((temp = getchar()) != '\n')
		putchar(temp);
}
char * str_back(char *s){
	int n = strlen(s);
	int j = 0;
	char temp[n];
	for(int i = 0;i<n;i++){
		temp[i] = s[i];
	}
	for(int i = n-1;i>=0;i--,j++){
		s[j] = temp[i];
	}
	return s;
}


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

#include <stdio.h>
#include <string.h>
void outtemp(void); //丢弃缓冲区中的多余的输入
char * str_del(char *s); //删除字符串的空格
int main(void)
{
	char temp[20];
	char * ptemp = temp;
	char ch = 'y';
	while(ch != 'q'){
		puts("请输入一个字符串(不要超过20个字符):");
		gets(ptemp);
		puts("删除空格后的结果:");
		puts(str_del(ptemp));
		puts("继续测试请按y,退出请按q:");
		ch = getchar();
		outtemp();
	}
    return 0;
}

void outtemp(void){
	char temp;
	while((temp = getchar()) != '\n')
		putchar(temp);
}
char * str_del(char *s){
	int n = strlen(s);
	int j = 0;
	char temp[n];
	for(int i = 0;i<n;i++){
		temp[i] = s[i];
	}
	for(int i = 0;i<n;i++){
		if(temp[i] != ' '){
			s[j] = temp[i];
			j++;
		}
	}
	s[j] = '\0';
	return s;
}

/*
 ============================================================================
10.编写一个程序,读取输入,直到输入10个字符串或是遇到EOF,由二者中最先被满足的那个终止读
取过程,这个程序可以为用户提供一个5个选项的菜单:输出初始字符串列表、按ASCII顺序输出字符
串、按长度递增顺序输出字符串、按字符串中第一个单词的长度输出字符串、退出。菜单可以循环,直
到用户输入退出指令。
 ============================================================================
 */

#include <stdio.h>
#include <string.h>
void outtemp(void); //丢弃缓冲区中的多余的输入
void show_string(char *string[],int num); //显示字符串
void ascii_string(char *string[],int num); //对字符串按照ascii码进行排序
void len_string(char *string[],int num); //按照字符串长递增进行排序
void word_string(char *string[],int num); //按照字符串第一个单词长度进行排序
int len_word(char *string,int num); //返回字符串中第一个单词的长度
char menu(void); //菜单的显示与选择控制
#define LIM 10    //字符串行数
#define SIZE 81   //字符串列数
int main(void){
	char ch = 'y'; //菜单选项
	char input[LIM][SIZE];
	char *ptr_gensis[LIM]; //原始字符串
	char *ptr_ascii[LIM];   //存放ascii码排序的指针
	char *ptr_len[LIM];     //存放按照字符串长度排序的指针
	char *ptr_word[LIM];    //存放使用第一个单词排序的指针

	int ct = 0; //输入计数

	printf("请输入字符串,最多10条:\n");
	while(ct < LIM && gets(input[ct]) != NULL && input[ct][0] != '\0'){
		ptr_gensis[ct] = input[ct];
		ptr_ascii[ct] = input[ct];
		ptr_len[ct] = input[ct];
		ptr_word[ct] = input[ct];
		ct++;
	}
	while(ch != 'e'){
		ch = menu();
		switch(ch){
		case 'a':
			printf("原始输入:\n");
			show_string(ptr_gensis,ct);
			break;
		case 'b':
			printf("按照ASCII码排序:\n");
			ascii_string(ptr_ascii,ct);
			show_string(ptr_ascii,ct);
			break;
		case 'c':
			printf("按照字符串递增排序:\n");
			len_string(ptr_len,ct);
			show_string(ptr_len,ct);
			break;
		case 'd':
			printf("按照字符串第一个单词长度排序:\n");
			word_string(ptr_word,ct);
			show_string(ptr_word,ct);
		case 'e':
			printf("退出\n");
			break;
		default:
			printf("谢谢使用\n");
		}
	}
    return 0;
}

void outtemp(void){
	char temp;
	while((temp = getchar()) != '\n')
		putchar(temp);
}

void show_string(char *string[],int num){
	for(int i = 0;i<num;i++){
		puts(string[i]);
	}
}
void ascii_string(char *string[],int num){
	char *temp;
	int top,seek;
	for(top= 0;top<num-1;top++){
		for(seek = top +1;seek <num;seek++){
			if(strcmp(string[top],string[seek]) > 0){
				//strcmp(s1,s2):比较s1和s2的ascii码顺序,
				//如果s1落后于s2,则返回正数,相同返回0,否则返回负数
				temp = string[top];
				string[top] = string[seek];
				string[seek] = temp;
			}
		}
	}
}

void len_string(char *string[],int num){
	char *temp;
	int top,seek;
	for(top = 0;top<num-1;top++){
		for(seek = top +1;seek < num;seek++){
			if(strlen(string[top]) > strlen(string[seek])){
				temp = string[top];
				string[top] = string[seek];
				string[seek] = temp;
			}
		}
	}
}
int len_word(char *string,int num){
	int i =0;
	char ch = string[i];
	while(i < num && ch != '\n' && ch != EOF && ch !=' '){
		i++;
		ch = string[i];
	}
	return i;
}
void word_string(char *string[],int num){
	char *temp;
	int top,seek;
	for(top = 0;top<num-1;top++){
		for(seek = top +1;seek < num;seek++){
			if(len_word(string[top],SIZE) < len_word(string[seek],SIZE)){
				temp = string[top];
				string[top] = string[seek];
				string[seek] = temp;
			}
		}
	}
}
char menu(void){
	char ch;
	puts("***********菜单列表****************");
	puts("a.显示原始字符串");
	puts("b.对字符串按照Ascii码排序");
	puts("c.按照字符串的长度进行递增排序");
	puts("d.按照字符串第一个单词长度进行排序");
	puts("e.退出程序");
	puts("**********************************");
	printf("请输入你的选择:");
	ch = getchar();
	outtemp();
	while(ch < 'a' || ch > 'e'){
		printf("错误的输入,请重新输入(可以输入选项为:a,b,c,d,e):");
		ch = getchar();
		outtemp();
	}
	return ch;
}

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

#include <stdio.h>
#include <string.h>
#include <ctype.h>
void outtemp(void); //丢弃缓冲区中的多余的输入

int main(void){
	char ch;
	int i = 0; //字符个数
	int j = 0; //字母个数
	int a = 0; //大写字母个数
	int b = 0; //小写字母个数
	int c = 0; //标点号符号数
	int d = 0; //数字字符个数
	int w = 0; //单词个数

	puts("请输入你要统计的字符串:");
	ch = getchar();
	while(ch != EOF){
		while(!isspace(ch)){
			if(isalpha(ch)) j++;
			if(islower(ch)) b++;
			if(isupper(ch)) a++;
			if(ispunct(ch)) c++;
			if(isdigit(ch)) d++;
			i++;
			ch = getchar();
		}
		w++;
		while(isspace(ch) && ch != EOF){
			putchar(ch);
			i++;
			ch = getchar();
		}
	}
	printf("所有字符:%d\t单词:%d\t字母:%d\n大写字母:%d\t小写字母:%d\t标点符号:%d\t数字:%d\t",i,w,j,a,b,c,d);
    return 0;
}

void outtemp(void){
	char temp;
	while((temp = getchar()) != '\n')
		putchar(temp);
}

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

#include <stdio.h>
#include <string.h>
int main(int argc,char *argv[]){
	for(int i = argc-1;i>0;i--){
		puts(argv[i]);
	}
    return 0;
}

/*
 ============================================================================
13.编写一个计算乘幂的基于命令行程序,第一个命令行参数为double类型数,作为幂的底数第二个
参数为整数,作为幂的指数
 ============================================================================
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
int main(int argc,char *argv[]){
	double a = atof(argv[1]);
	int b = atoi(argv[2]);
	printf("%.2f^%d = %.2f",a,b,pow(a,b));
    return 0;
}

/*
 ============================================================================
14.使有字符分类函数实现atoi()函数
 ============================================================================
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
int my_atoi(char *s); //将char*s转换为一个整数值
int main(int argc,char *argv[]){
	int a = my_atoi(argv[1]);
	printf("%d",a);
    return 0;
}

int my_atoi(char *s){
	int a = 0;
	int b;
	int k;
	int n = strlen(s);
	for(int i = 0,k = n-1;i<n;i++,k--){
		b = s[i] - 48;
		a += b*pow(10,k);
	}
	return a;
}


/*
 ============================================================================
15.编写一个程序,其功能是读取输入,直到遇到文件节尾,并把文件显示出来,要求程序可以识别
并执行下面的命令参数:
-p 按照原样显示输入
-u 把输入全部转换为大写
-l 把输入全部转换为小写
 ============================================================================
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define SIZE 81
int main(int argc,char *argv[]){
	char f = argv[1][1];   //读取参数
	char ch;
	while((ch = getchar()) != EOF){
		if(f=='p') putchar(ch);
		if(f=='u') putchar(toupper(ch));
		if(f=='l') putchar(tolower(ch));
	}
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值