《C Primer Plus》(第6版)编程练习——第11章

第1题

#include <stdio.h>
#define LEN 10
char* getnchar(char* str, int n);

int main(void)
{
	char input[LEN];
	char* check;

	check = getnchar(input, LEN - 1);
	if (check == NULL)
		puts("Input failed");
	else
		puts(input);
	puts("Done.");

	return 0;
}

char* getnchar(char* str, int n)
{
	char ch;
	int i;

	for (i = 0; i < n; i++)
	{
		ch = getchar();
		if (ch != EOF)
			str[i] = ch;
		else
			break;
	}
	if (ch == EOF)
		return NULL;
	else
	{
		str[i] = '\0';
		return str;
	}	
}

第2题

#include <stdio.h>
#include <ctype.h>
#define LEN 10
char* getnchar(char* str, int n);

int main(void)
{
	char input[LEN];
	char* check;

	check = getnchar(input, LEN - 1);
	if (check == NULL)
		puts("Input failed");
	else
		puts(input);
	puts("Done.");

	return 0;
}

char* getnchar(char* str, int n)
{
	char ch;
	int i;

	for (i = 0; i < n; i++)
	{
		ch = getchar();
		if (ch != EOF && !isspace(ch))
			str[i] = ch;
		else
			break;
	}
	if (ch == EOF)
		return NULL;
	else
	{
		str[i] = '\0';
		return str;
	}	
}

第3题

#include <stdio.h>
#include <ctype.h>
#define LEN 80
char* getword(char* str);

int main(void)
{
	char input[LEN];

	while (getword(input) != NULL)
		puts(input);
	puts("Done.\n");

	return 0;
}

char* getword(char* str)
{
	char ch;
	char* orig = str;

	// 跳过第1个非空白字符前面的所有空白
	while ((ch = getchar()) != EOF && isspace(ch))
		continue;
	if (ch == EOF)
		return NULL;
	else
		*str++ = ch;   // 单词的第1个字符
	// 读入单词的剩余部分
	while ((ch = getchar()) != EOF && !isspace(ch))
		*str++ = ch;
	*str = '\0';
    // 丢弃输入行中的其余字符
	if (ch == EOF)
		return NULL;
	else
	{
		while (ch != '\n')
			ch = getchar();
		return orig;
	}
}

第4题

#include <stdio.h>
#include <ctype.h>
#define LEN 10
char* getword(char* str, int n);

int main(void)
{
	char input[LEN];

	while (getword(input, LEN - 1) != NULL)
		puts(input);
	puts("Done.\n");

	return 0;
}

char* getword(char* str, int n)
{
	char ch;
	int i = 0;

	// 跳过第1个非空白字符前面的所有空白
	while ((ch = getchar()) != EOF && isspace(ch))
		continue;
	if (ch == EOF)
		return NULL;
	else
		str[i++] = ch;   // 单词的第1个字符
	// 读入单词的剩余部分
	while ((ch = getchar()) != EOF && !isspace(ch) && i < n)
		str[i++] = ch;
	str[i] = '\0';
    // 丢弃输入行中的其余字符
	if (ch == EOF)
		return NULL;
	else
	{
		while (ch != '\n')
			ch = getchar();
		return str;
	}
}

第5题

#include <stdio.h>
#define LEN 81
char* mystrchr(const char* str, char ch);

int main(void)
{
	char input[LEN];
	char c;

	printf("Enter a string (less than %d characters):\n", LEN - 1);
	printf("To stop, press the Enter key at a line's start.\n");
	while (fgets(input, LEN, stdin) && input[0] != '\n')
	{
		printf("Enter a character you want to find: ");
		c = getchar();
		while (getchar() != '\n')
			continue;
		if (mystrchr(input, c))
			printf("%c is in the string: %s\n", c, input);
		else
			printf("%c is not in the string: %s\n", c, input);
		printf("Enter next string (less than %d characters):\n", LEN - 1);
		printf("To stop, press the Enter key at a line's start.\n");
	}
	puts("Done.\n");

	return 0;
}

char* mystrchr(const char* str, char ch)
{
	while (*str)     // 若不是空字符
	{
		if (*str == ch)
			return (char *)str;
		str++;
	}
	if (*str == ch)  // 若空字符就是要找的字符
		return (char *)str;   // 通过强制类型转换取消const
	else
		return NULL;
}

第6题

#include <stdio.h>
#include <string.h>
#define LEN 81
_Bool is_within(const char* str, char c);
char* s_gets(char* st, int n);

int main(void)
{
	char input[LEN];
	char ch;
	int found;

	printf("Enter a string (less than %d characters):\n", LEN - 1);
	while (s_gets(input, LEN) && input[0] != '\0')
	{
		printf("Enter a character: ");
		ch = getchar();
		while (getchar() != '\n')
			continue;
		found = is_within(input, ch);
		if (found == 0)
			printf("%c not found in string.\n", ch);
		else
			printf("%c found in string %s\n", ch, input);
		printf("Enter next string: ");
	}
	puts("Done.\n");

	return 0;
}

_Bool is_within(const char* str, char ch)
{
	while (*str != ch && *str != '\0')
		str++;

	return *str;
}

char* s_gets(char* st, int n)
{
	char* ret_val;
	char* find;

	ret_val = fgets(st, n, stdin);
	if (ret_val)
	{
		find = strchr(st, '\n');
		if (find)
			*find = '\0';
		else
			while (getchar() != '\n')
				continue;
	}

	return ret_val;
}

第7题

#include <stdio.h>
#define LEN 81
char* mystrncpy(char* s1, const char* s2, int n);
char* s_gets(char* st, int n);

int main(void)
{
	char str1[LEN];
	char str2[LEN];
	int num;

	puts("This program can copy n characters of string2 to string1.");
	printf("Enter string2 (less than %d characters):\n", LEN - 1);
	while (s_gets(str2, LEN) && str2[0] != '\0')
	{
		printf("Enter numbers of characters to copy: ");
		scanf("%d", &num);            // 换行符还在缓冲区
		while (getchar() != '\n')     // 清除缓冲区剩余字符
			continue;
		mystrncpy(str1, str2, num);
		puts("String 1:");
		puts(str1);
		puts("Enter next string2:");
	}
	puts("Done.\n");

	return 0;
}

char* mystrncpy(char* s1, const char* s2, int n)
{
	int i;

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

	return s1;
}

char* s_gets(char* st, int n)
{
	char* ret_val;
	int i = 0;

	ret_val = fgets(st, n, stdin);
	if (ret_val)
	{
		while (st[i] != '\n' && st[i] != '\0')
			i++;
		if (st[i] == '\n')
			st[i] = '\0';
		else
			while (getchar() != '\n')
				continue;
	}

	return ret_val;
}

第8题

#include <stdio.h>
#include <string.h>
#define LEN 81
char* string_in(const char* s1, const char* s2);
char* s_gets(char* st, int n);

int main(void)
{
	char str1[LEN];
	char str2[LEN];
	char* add;

	puts("This program can check if string1 contains string2.");
	printf("Enter string1 (less than %d characters):\n", LEN - 1);
	while (s_gets(str1, LEN) && str1[0] != '\0')
	{
		printf("Enter string2: ");
		s_gets(str2, LEN);
		add = string_in(str1, str2);
		if (add)
			printf("%s contains %s at %p.\n", str1, str2, add);
		else
			printf("%s doesn't contain %s.\n", str1, str2);
		puts("Enter next string1:");
	}
	puts("Done.\n");

	return 0;
}

char* string_in(const char* s1, const char* s2)
{
	int l2 = strlen(s2);
	int tries;
	int nomatch = 1;

	tries = strlen(s1) + 1 - l2;
	if (tries > 0)
		while ((nomatch = strncmp(s1, s2, l2)) && tries--)
			s1++;
	if (nomatch)
		return NULL;
	else
		return (char*)s1;  // 通过强制类型转换取消const
}

/*  不用库函数strncmp(),不须包含string.h头文件
char* string_in(const char* s1, const char* s2)
{
	int i = 0;
	int j;

	while (s1[i])
	{
		j = 1;
		if (s1[i] == s2[0])
		{
			while (s1[i + j] && s2[j] && s1[i + j] == s2[j])
				j++;
			if (s2[j] == '\0')
				return (char*)&s1[i];   // 通过强制类型转换取消const
		}
		i++;
	}

	return NULL;
}
*/

char* s_gets(char* st, int n)
{
	char* ret_val;
	int i = 0;

	ret_val = fgets(st, n, stdin);
	if (ret_val)
	{
		while (st[i] != '\n' && st[i] != '\0')
			i++;
		if (st[i] == '\n')
			st[i] = '\0';
		else
			while (getchar() != '\n')
				continue;
	}

	return ret_val;
}

第9题

#include <stdio.h>
#include <string.h>
#define LEN 81
char* str_inv(char* st);
char* s_gets(char* st, int n);

int main(void)
{
	char str[LEN];

	puts("This program can invert a string.");
	printf("Enter a string (less than %d characters):\n", LEN - 1);
	while (s_gets(str, LEN) && str[0] != '\0')
	{
		puts("The original string:");
		puts(str);
		str_inv(str);
		puts("The inverted string:");
		puts(str);
		puts("\nEnter next string:");
	}
	puts("Done.\n");

	return 0;
}

char* str_inv(char* st)
{
	int i;
	int len = strlen(st);
	char temp;

	for (i = 0; i < len / 2; i++)
	{
		temp = st[i];
		st[i] = st[len - 1 - i];
		st[len - 1 - i] = temp;
	}

	return st;
}


char* s_gets(char* st, int n)
{
	char* ret_val;
	int i = 0;

	ret_val = fgets(st, n, stdin);
	if (ret_val)
	{
		while (st[i] != '\n' && st[i] != '\0')
			i++;
		if (st[i] == '\n')
			st[i] = '\0';
		else
			while (getchar() != '\n')
				continue;
	}

	return ret_val;
}

第10题

#include <stdio.h>
#define LEN 81
void drop_space(char* st);
char* s_gets(char* st, int n);

int main(void)
{
	char str[LEN];

	printf("Enter a string (less than %d characters):\n", LEN - 1);
	while (s_gets(str, LEN) && str[0] != '\0')
	{
		drop_space(str);
		puts("The string after removing the spaces:");
		puts(str);
		puts("\nEnter next string (or just Enter to quit):");
	}
	puts("Done.\n");

	return 0;
}

void drop_space(char* st)
{
	char* pos;

	while (*st)
	{
		if (*st == ' ')
		{
			pos = st;
			do
			{
				*pos = *(pos + 1);
				pos++;
			} while (*pos);
		}
		else
			st++;
	}
}

char* s_gets(char* st, int n)
{
	char* ret_val;
	int i = 0;

	ret_val = fgets(st, n, stdin);
	if (ret_val)
	{
		while (st[i] != '\n' && st[i] != '\0')
			i++;
		if (st[i] == '\n')
			st[i] = '\0';
		else
			while (getchar() != '\n')
				continue;
	}

	return ret_val;
}

第11题

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define LIM 10
#define SIZE 81
void menu(void);
void opt_a(const char st[][SIZE], int c);
void opt_b(const char st[][SIZE], int c);
void opt_c(const char st[][SIZE], int c);
void opt_d(const char st[][SIZE], int c);
char* s_gets(char* st, int n);

int main(void)
{
	char str[LIM][SIZE];
	int ct = 0;
	char res;

	printf("Enter up to %d strings:", LIM);
	while (ct < LIM)
	{
		printf("Enter string #%d:\n", ct + 1);
		if (s_gets(str[ct], SIZE) == NULL)
			break;
		ct++;
	}
	menu();
	while ((res = getchar()) && res != 'q')
	{
		if (res == '\n')
			continue;
		while (getchar() != '\n')
			continue;
		if (res == 'a')
			opt_a(str, ct);
		else if (res == 'b')
			opt_b(str, ct);
		else if (res == 'c')
			opt_c(str, ct);
		else if (res == 'd')
			opt_d(str, ct);
		else
			puts("Enter a, b, c, d or q:");
		menu();
	}
	puts("Bye.");

	return 0;
}

void menu(void)
{
	puts("Enter the letter of corresponding:");
	puts("a) print list of strings");
	puts("b) print strings in an order of ASCII code");
	puts("c) print strings in an order of increasing length");
	puts("d) print strings as long as the first word in the string");
	puts("q) quit");
}

void opt_a(const char st[][SIZE], int num)
{
	int i;
	
	puts("\nList of strings:");
	for (i = 0; i < num; i++)
		puts(st[i]);
	puts("\n");
}

void opt_b(const char st[][SIZE], int num)
{
	char* ptst[LIM];
	char* temp;
	int i, top, seek;
	
	for (i = 0; i < num; i++)
		ptst[i] = (char*)st[i];
	for (top = 0; top < num - 1; top++)
		for (seek = top + 1; seek < num; seek++)
			if (strcmp(ptst[top], ptst[seek]) > 0)
			{
				temp = ptst[top];
				ptst[top] = ptst[seek];
				ptst[seek] = temp;
			}
	puts("\nStrings in an order of ASCII code:");
	for (i = 0; i < num; i++)
		puts(ptst[i]);
	puts("\n");
}

void opt_c(const char st[][SIZE], int num)
{
	char* ptst[LIM];
	char* temp;
	int i, top, seek;

	for (i = 0; i < num; i++)
		ptst[i] = (char*)st[i];
	for (top = 0; top < num - 1; top++)
		for (seek = top + 1; seek < num; seek++)
			if (strlen(ptst[top]) > strlen(ptst[seek]))
			{
				temp = ptst[top];
				ptst[top] = ptst[seek];
				ptst[seek] = temp;
			}
	puts("\nStrings in an order of increasing length:");
	for (i = 0; i < num; i++)
		puts(ptst[i]);
	puts("\n");
}

void opt_d(const char st[][SIZE], int num)
{
	int i;
	int wordlen[LIM];  // 统计第一个单词长度
	char* pt;
	
	for (i = 0; i < num; i++)
	{
		wordlen[i] = 0;
		pt = (char*)st[i];
		while (isspace(*pt))          // 跳过单词前的空格
			pt++;
		while (!isspace(*pt) && *pt)  // 计算单词长度
		{
			wordlen[i]++;
			pt++;
		}
	}
	puts("\nStrings as long as the first word in the string:");
	for (i = 0; i < num; i++)
	{
		for (pt = (char*)st[i]; wordlen[i] > 0; pt++, wordlen[i]--)  // 按单词长度打印字符串
			putchar(*pt);
		putchar('\n');
	}
	puts("\n");
}

char* s_gets(char* st, int n)
{
	char* ret_val;
	int i = 0;

	ret_val = fgets(st, n, stdin);
	if (ret_val)
	{
		while (st[i] != '\n' && st[i] != '\0')
			i++;
		if (st[i] == '\n')
			st[i] = '\0';
		else
			while (getchar() != '\n')
				continue;
	}

	return ret_val;
}

第12题

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

int main(void)
{
	int c;
	int n_words = 0;
	int n_upper = 0;
	int n_lower = 0;
	int n_punct = 0;
	int n_digit = 0;
	_Bool inword = 0;

	printf("Enter text to be analyzed (EOF to terminate):\n");
	while ((c = getchar()) != EOF)
	{
		if (isupper(c))
			n_upper++;
		else if (islower(c))
			n_lower++;
		else if (ispunct(c))
			n_punct++;
		else if (isdigit(c))
			n_digit++;
		if (!isspace(c) && inword == 0)
		{
			inword = 1;
			n_words++;
		}			
		if (isspace(c) && inword == 1)
			inword = 0;		
	}
	printf("\nwords = %d, uppercase = %d, lowercase = %d, punctuation = %d,"
		" digits = %d\n", n_words, n_upper, n_lower, n_punct, n_digit);

	return 0;
}

第13题

#include <stdio.h>
int main(int argc, char* argv[])
{
	int i;

	for (i = argc - 1; i > 0; i--)
		printf("%s ", argv[i]);
	printf("\n");

	return 0;
}

第14题

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

int main(int argc, char* argv[])
{
	double num, exp;

	if (argc != 3)
		printf("Usage: %s number exponent\n", argv[0]);
	else
	{
		num = atof(argv[1]);
		exp = atof(argv[2]);
		printf("%f to the %f power = %g\n", num, exp, pow(num, exp));
	}

	return 0;
}

第15题

#include <stdio.h>
#include <ctype.h>
#define LEN 30
int myatoi(char*);

int main(void)
{
	char str[LEN];
	int num;

	printf("Enter a string (only digits):\n");
	gets_s(str, LEN);
	num = myatoi(str);
	printf("String %s coverted to number is: %d\n", str, num);

	return 0;
}

int myatoi(char* st)
{
	int val = 0;

	while (*st)
	{
		if (!isdigit(*st))
			return 0;
		else
		{
			val = val * 10 + (*st - '0');
			st++;
		}
	}
	return val;
}

第16题

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

int main(int argc, char* argv[])
{
	char mode = 'p';
	int ok = 1;
	int ch;

	if (argc > 2)
	{
		printf("Usage: %s [-p | -u | -l]\n", argv[0]);
		ok = 0;
	}
	else if (argc == 2)
	{
		if (argv[1][0] != '-')
		{
			printf("Usage: %s [-p | -u | -l]\n", argv[0]);
			ok = 0;
		}
		else
			switch (argv[1][1])
			{
				case 'p':
				case 'u':
				case 'l': mode = argv[1][1];
					      break;
				default:  printf("%s is an invalid flag; using default flag (-p).\n", argv[1]);				    
			}
	}
	if (ok)
		while ((ch = getchar()) != EOF)
		{
			switch (mode)
			{
			case 'p': putchar(ch);
				      break;
			case 'u': putchar(toupper(ch));
				      break;
			case 'l': putchar(tolower(ch));
			}
		}

	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值