C primer plus 第六版 第十一章 编程练习 个人答案

学习C语言试图入门
下面的编程练习仅为个人想法
如果有更好的思路、建议 或 发现错误,欢迎在评论区指出。


  1. (其实是因为源码找不到了…)

/* 11.13 -- 2 */
#include <stdio.h>
#include <string.h>
char * in_n(char *, int n);
void duqv(void);
int main(void)
{
    
    
	char shuzu[30];
	int n = 30;
	char * ptr;
	ptr = in_n(shuzu, n);
	puts(ptr);
	return 0;
}

char *in_n(char *st, int n)
{
    
    
	char *get;
	char i = 0;
	char *ptr;
	get = fgets(st, n, stdin);
	while (st[i] != ' ' && st[i] != '\t' && st[i] != '\n')
	{
    
    
		i++;
	}
	if (st[i] == ' ')
	{
    
    
		st[i] = '\0';
	}
	else if (st[i] == '\t')
	st[i] = '\0';
	else if (st[i] == '\n')
	st[i] = '\0';
	if (strchr(st+i+1, '\n') == 0)
		duqv();
	
	return get;
}

void duqv(void)
{
    
    
	while (getchar() != '\n')
		continue;
}

3、4.
(3、4题类似,第3题源码被我改成第四题的了,故只放第4题)

/* 11.13 --3\4 */
#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define SIZE 20
char *in_word(char *, int n);
int main(void)
{
    
    
	char str[SIZE];
	char *ptr;
	int count = 0;
	
	ptr = in_word(str,SIZE);
	while (*ptr != '\0') // 赋给字符数组 
	{
    
    
		str[count] = *ptr;
		count++;
		ptr++;
	}
	str[count] = '\0';  // 将str原来的字符用 '\0'截断 
	puts(str);
}
char *in_word(char *st,int n)
{
    
    
	char *ptr;
	int i = 0;
	ptr = fgets(st, n, stdin);
	while (st[i] == ' ') // 跳过开头空白字符 
	{
    
    
		i++;
		ptr++;
	}
	while (!(isspace(st[i])))  
	{
    
    
		i++;
	}
	st[i] = '\0';
	
	return ptr;	
}
/* 11.13 --5 */
#include <stdio.h>
#include <ctype.h>

#define SIZE 80

char *strcm(char *,char );

int main(void)
{
    
    
	char *ptr;
	char str[SIZE] = "hello world! I blog at CSDN!"; 
	char m;
	printf("输入一个你要查找的值:\n");			
	scanf("%c",&m);							// m读取要查找的数
	do     							
	{
    
    
		ptr = strcm(str,m);
		puts(ptr);						// 返回指针并且打印
		printf("输入一个你要查找的值:\n");     
	}while (scanf("%c",&m) == 1); 
	return 0;
}

char *strcm(char *st,char n)
{
    
    
	getchar(); 				//读取 scanf 带来的回车
	char *res;
	res = st;
	
	while (*res != '\0')
	{
    
    
	if (*res == n)
	{
    
    
	return res;
	break;
	}
	else
	res++;
	}
	
	if (*res == '\0')
	return NULL;

}
#include <stdio.h>
int is_within(const char *,char);
int main(void)
{
    
    
	char str[] = "hello world! I blog at CSDN!"; 
	char m ;
	int i;
	printf("输入你要查找的字符:\n");
	if(scanf("%c",&m) == 1)
	{
    
    
	do
	{
    
    
		getchar();
		i = is_within(str, m);
		printf("%d\n",i);
		printf("输入你要查找的字符:\n");
	}while (scanf("%c",&m) == 1); 
	}
}
int is_within(const char *st,char n)
{
    
    
	const char *ptr;
	ptr = st;
	
	while (*ptr != '\0')
	{
    
    
		if (*ptr == n)
			break;
		else 
			ptr++;
	}

	
	if (*ptr == '\0')
		return 0;
	else 
		return 1;
}
/* 11.13 -- 7 mystrncpy */
#include <stdio.h>

#define SIZE 10

char * mystrncpy (char *, char *,int n);
char *s_gets(char *st, int n);

int main(void)
{
    
    
	char str2[SIZE];
	int n = SIZE;
	char str1[SIZE];
	printf("循环输入你要读取拷贝的值:\n");
	while (s_gets(str1, n) != NULL)   
	{
    
    
		mystrncpy(str2, str1, n);
		puts(str2);
	}

	
	return 0;
}
char * mystrncpy (char *st, char *ct,int n)
{
    
    
	int count = 0;
	while (*ct != '\0' && count < n)
	{
    
    
		*st = *ct;
		ct++;
		st++;
		count++;
	}
	
	if (*ct == '\0')
		*st ='\0';
}

char *s_gets(char *st, int n)
{
    
    
	char *ct;
	int i = 0;
	
	ct = fgets(st,n,stdin);
	
	while (st[i] != '\0' && st[i] != '\n')
	i++;
	
	if (st[i] == '\n')
		st[i] = '\0';
	else
		while (getchar() != '\n')
			continue;
	
	return ct;
}
/* 11.13 --8*/
#include <stdio.h>
#include <string.h>

#define SIZE 80

char *s_gets(char *st, int n);
char *string_in(char *,char *);
int main(void)
{
    
    
	char str1[SIZE];
	char str2[SIZE];
	char *ptr;
	while (s_gets(str1,SIZE) != NULL && s_gets(str2,SIZE) != NULL)
	{
    
    
		ptr = string_in(str1, str2);
		puts(ptr);
	}
	
	return 0;
}
char *string_in(char *pt1,char *pt2)
{
    
    
	char *pt;
	int i = 0;
	int j = 0;
	int m =strlen(pt2);
	char store[3];
	 
	store[2] = '\0';	// !!!  注意这里比较的是 字符串与字符串 所以需要在该字符的末尾加上 '\0'的结束符号 
	pt = pt1;
	
	for (;pt1[i+m] != '\0';i++)
	{
    
    
		strncpy(store,pt1+i,m);
		if ((strcmp(store,pt2) == 0))
		{
    
    
			break;
		}
		
	}
	if (pt1[i+m] == '\0')
	{
    
    
		printf("未找到");
		return NULL;
	}else
	{
    
    
		printf("找到了如下:\n");
		return pt+i;
	}
	
}
char *s_gets(char *st, int n)
{
    
    
	char *ct;
	int i = 0;
	
	ct = fgets(st,n,stdin);
	
	while (st[i] != '\0' && st[i] != '\n')
	i++;
	
	if (st[i] == '\n')
		st[i] = '\0';
	else
		while (getchar() != '\n')
			continue;
	
	return ct;
}



/* 11.13 -- 9 */
#include <stdio.h>
#include <string.h>
#include <stdlib.h> 
char *fanxv(char *st, int n);
int main(void)
{
    
    
	int i;
	char str[] = "abcdefg";       //  用默认字符串代替了循环输出,我承认,我懒了 
	char *ptr;
	
	ptr = fanxv(str, strlen(str)+1);
	puts(ptr);
	return 0;
}

char *fanxv(char *st,int n)
{
    
    
	char ct[8];
	int i = 0;
	int j = 0;
	
	ct[n-1] = '\0';
	
	for (;i<7;i++)
	{
    
    
		ct[i]=st[6-i];
	}
	
	for (;j<7;j++)
	{
    
    
		st[j]= ct[j];
	} 
	return st;
}
/* 11.13 --10 */
#include <stdio.h>
#include <string.h>
char *hanshu(char *);
int main(void)
{
    
    
	char st[30];
	char *ptr;
	printf("输入字符串,回车结束:\n"); 
	while (fgets(st,30,stdin) && st[0] != '\n')
	{
    
    
		ptr = hanshu(st);
		puts(ptr);
	}
	printf("Done!");
}

char *hanshu(char *st)
{
    
    
	int i = 0;
	char * ct;
	int j;
	
	ct = st;
	while (st[i] != '\0')
	{
    
    
		if (st[i] == ' ')
		{
    
    
			for (j = i;st[j] != '\0';j++)
			{
    
    
				st[j] = st[j+1];
			}
		}
		i++;
	}
	return ct;
}
/* 11.13 -- 11 */
#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define SIZE 10
#define LIST 30

int caidan(void);
char *s_gets(char *,int n);
void project1(char (*st)[LIST], int n);
void project2(char (*st)[LIST], int n);
void Project3(char (*st)[LIST], int n);
void project4(char (*st)[LIST], int n);
int strle(char *st);

int main(void)
{
    
    
	char str[SIZE][LIST];
	char m;
	int i = 0;
	char *ct;
	int count;
	
	while (caidan() &&scanf("%c",&m) && m != 'q')
	{
    
    
		printf("输入%d条字符串:\n",SIZE);
		
		getchar();
		while(i < SIZE)
		{
    
    
			ct = s_gets(str[i],LIST);
			printf("str[%d] = ",i);
			puts(ct);
			if (ct != NULL)
			{
    
    
				count = i;
				i++;
				continue;
			}
			break;
		}
		switch (m)
		{
    
    
			case 'a':
				project1(str,count);
				break;
			case 'b':
				project2(str,count);
				break;
			case 'c':
				project3(str,count);
				break;
			case 'd':
				project4(str,count);
				break;
		}
	}
	
	printf("Done!"); 
	return 0;
}

char *s_gets(char *st, int n)
{
    
    
	char *ct;
	int i = 0;
	
	ct = fgets(st,n,stdin);
	
	while (st[i] != '\0' && st[i] != '\n')
	i++;
	
	if (st[i] == '\n')
		st[i] = '\0';
	else
		while (getchar() != '\n')
			continue;
	
	return ct;
}

int caidan(void)
{
    
    
	printf("-----------------------------------------------\n");
	printf("你需要什么?\n                                 \n");
	printf("a)打印源字符串列表    b)以ACSCLL顺序打印     \n");
	printf("c)以长度递增打印      d)按第一个单词长度打印 \n");
	printf("q)quit                                       \n");
	printf("-----------------------------------------------\n");
	return 1;
}

void project1(char (*st)[LIST],int n)
{
    
    
	int i = 0;
	for (i = 0; i<= n ;i++)
	puts(st[i]);
}

void project2(char (*st)[LIST],int n)
{
    
    
	int i,j;
	char *min;
	char temp[LIST];
	for (i = 0;i <= n;i++)
	{
    
    
		for (j = i,min = st[i];j <= n;j++)
		{
    
    
			if (strcmp(min, st[j]) > 0)
			min = st[j];
		}
		strcpy(temp,st[i]);
		strcpy(st[i],min);
		strcpy(min,temp);
	}
	for (i = 0;i <= n;i++)
	puts(st[i]);
}

void project3(char (*st)[LIST], int n)
{
    
    
	int i = 0;
	char *min;
	int k;
	int j = 0;
	char temp[LIST];
	for (i = 0; i <= n;i++)
	{
    
    
		for (j = i, min = st[i];j <= n;j++)
		{
    
    
			if (strlen(st[j]) < strlen(min))
			min = st[j];
		}
		strcpy(temp,st[i]);
		strcpy(st[i],min);
		strcpy(min,temp);	
		
	}
	for ( k = 0;k <= n;k++)
	puts(st[k]);
}
 
void project4(char (*st)[LIST],int n)
{
    
    
	char *name[LIST];
	char *min;
	char temp[LIST];
	int i,j,k;
	j = 0;
	for (i=0;i<=n;i++)
	name[i] = st[i];
	
	for (i = 0;i <= n;i++)
	{
    
    
		for (j = i, min = name[i];j <= n;j++)
		{
    
    
			if (strle(name[j]) < strle(min))
			{
    
    
				//printf(" %d < %d \n",strle(name[j]),strle(min)) ;
				min = name[j];
				printf("%s\n",min);
			}
		}
		strcpy(temp,name[i]);
		strcpy(name[i],min);
		strcpy(min,temp);		
	}
	for ( k = 0;k <= n;k++)
		puts(name[k]);
}

int strle(char *st)
{
    
    
	int i,j;
	int count = 0;
	while (!(isspace(*st)) && *st != '\0')
	{
    
    
		st = st+1;
		count++;
	}
	return count;
}
/* 11.13 --12 */
#include <stdio.h>
#include <string.h>
int main(void)
{
    
    
	char name[50];
	int n_up = 0;
	int n_low = 0;
	int n_w = 0;
	int n_s = 0;
	int n_p = 0;
	int flag = 1;   //flag 标识前一位是否空格
	char ch;
	
	while ((ch = getchar()) != EOF)
	{
    
    
		if (isupper(ch))
			{
    
    
				n_up++;
				flag = 1;
			}
		else if (islower(ch))
			{
    
    
				n_low++;
				flag = 1;
			}
		else if (ispunct(ch))
			{
    
    
				n_p++;
				flag = 1;
			}
		else if (isdigit(ch))
			{
    
    
				n_s++;
				flag = 1;
			}
		else if (isspace(ch) && flag == 1)
			{
    
    
				n_w++;
				flag = 0;
			}
	}
	printf("小写字母数 = %d, 大写字母数 = %d\n",n_low,n_up);
	printf("共有字母数 = %d,标点符号数 = %d\n",n_w,n_p);
	printf("共有数字   = %d",n_s);
	getchar();
	return 0;
}
/*11.13 --13*/
#include <stdio.h>
int main(int argc, char *argv[])
{
    
    
	int count;
	for (count = argc-1;count > 0;count--)
	{
    
    
		printf("%s ",argv[count]);
	}
	return 0;
}
/* 11.13 --14 */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(int argc, char *argv[])
{
    
    
	double m;
	m = atof(argv[1]);
	int n;
	n = atoi(argv[2]);
	printf("%lf",pow(m,n));
}
/* 11.13 -- 15 */
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int main(int argc, char *argv[])
{
    
    
	int value;
	char *end;
	char *ptr;
	ptr = argv[1];
	
	while(*ptr != '\0')
	{
    
    
		if ((toupper(*ptr) < 'Z' && toupper(*ptr) > 'A'))
		{
    
    
			printf("0");
			break;
		}
		ptr++;
	}
	
	if (*ptr == '\0')
	{
    
    
		value = strtol(argv[1], &end, 16);
		printf("%ld",value);
	}
	return 0;
}
/* 11.15 --16 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define SIZE 100

int main(int argc,char *argv[])
{
    
    
	char str[SIZE];
	int i = 0;
	int len;
	
	printf("Enter a string:");
	
	fgets(str,SIZE, stdin);
	
	
	if (strcmp("-u",argv[1]) == 0)
	{
    
    
		while (str[i] != '\0')
		{
    
    
			str[i] = toupper(str[i]);
			i++;
		}
		
		fputs(str, stdout);
	}
	else if (strcmp("-l",argv[1]) == 0)
	{
    
    
		while (str[i] != '\0')
		{
    
    
			str[i] = tolower(str[i]);
			i++;
		}
		
		fputs(str, stdout);
	}
	else
	{
    
    
		fputs(str, stdout);
	}
	return 0;
}

完成。

写编程练习题时,遇见很多bug,有时候为了找一个问题想很久。
下面做一个小总结以后自己写代码的过程中尽量不再犯,

  1. 进入循环的入口条件,如&&、|| 、数组中特定的索引变更

  2. 字符串的末尾’\0’的出现,有关字符串末尾‘\0’的拼接问题。
    (strcmp的比较等)

  3. 循环问题中有关函数的数据的初始化.

暂时就想到这么多,如果还有继续补充。

猜你喜欢

转载自blog.csdn.net/qq_45469783/article/details/104515264
今日推荐