我的博客主要记录一下自己的学习代码过程。
大家可以在自己电脑上运行看看。
编译器为VS2019
最近事情比较多,没有每道题都写了。
第11章的运行结果如下:
11.1
#include <stdio.h>
#define SIZE 24
char* get_char(char* ch, int n);
int main(void)
{
char test[SIZE];
get_char(test, SIZE);
fputs(test, stdout);
return 0;
}
char* get_char(char* ch, int n)
{
puts("请输入内容:");
fgets(ch, SIZE - 1, stdin);
return ch;
}
运行结果:
只能储存24个字符(包括最后的\n)
11.2
#include <stdio.h>
#define SIZE 24
char* get_char(char* ch, int n);
int main(void)
{
char test[SIZE];
get_char(test, SIZE);
int i;
for (i = 0;i < SIZE;i++)
{
if (test[i] != ' ' && test[i] != '\t')
printf("%c", test[i]);
else
break;
}
return 0;
}
char* get_char(char* ch, int n)
{
puts("请输入内容:");
fgets(ch, SIZE , stdin);
return ch;
}
运行结果:
11.3
跟第8章第4题类似,只不过这里是放到数组里面。
#include <stdio.h>
#include <ctype.h>
#define SIZE 24
char* get_char(char* ch);
int main(void)
{
char test[SIZE];
get_char(test);
return 0;
}
char* get_char(char* ar)
{
int i = 0, j = 0;
int ch, ch0;
ch = ch0 = 0;
puts("请输入内容:");
while ((ch = getchar()) != EOF) {
if (isalpha(ch))
{
ar[i] = ch;
i++;
}
else if (isalpha(ch0))
break;
ch0 = ch;
}
puts("第一个单词为");
for (j = 0;j < i;j++)
printf("%c", ar[j]);
return ar;
}
运行结果:
11.4
设置只接受前5个字符
#include <stdio.h>
#include <ctype.h>
#define SIZE 24
char* get_char(char* ch,int n);
int main(void)
{
char test[SIZE];
get_char(test,5);
return 0;
}
char* get_char(char* ar,int n)
{
int i = 0, j = 0;
int ch, ch0;
ch = ch0 = 0;
puts("请输入内容:");
while ((ch = getchar()) != EOF) {
if (isalpha(ch))
{
ar[i] = ch;
i++;
}
else if (isalpha(ch0))
break;
if (i >= n)
break;
ch0 = ch;
}
puts("第一个单词为");
for (j = 0;j <i;j++)
printf("%c", ar[j]);
return ar;
}
运行结果:
11.5
#include <stdio.h>
#include <ctype.h>
#define SIZE 24
char* get_char(char* ar, char ch);
int main(void)
{
char test[SIZE], ch;
puts("请输入一段字符.");
fgets(test, SIZE, stdin);
char* ptr;
while (1)
{
puts("你要查找的字符串为?");
ch = getchar();
ptr = get_char(test, ch);
if (ptr != NULL)
{
puts("要查找的字符和它的地址分别为");
printf("%-5c,%p\n", *ptr, ptr);
}
else
puts("要查找的字符不在输入的字符串内。");
while (getchar() != '\n')
continue;
}
return 0;
}
char* get_char(char* ar, char ch)
{
char* ptr;
ptr = ar;
while (*ptr != '\0')
{
if (*ptr == ch)
return ptr;
ptr++;
}
return NULL;
}
运行结果:
11.6
这里答案用了C99新增的的_Bool类型的,但是在我的编译器上用不了。
_Bool is_within(const char * str, char ch)
{
while (*str != ch && *str != '\0')
str++;
return *str; /* = 0 if \0 reached, non-zero otherwise */
}
11.7
#include <stdio.h>
#define SIZE 10
#define N 5
char* mystrncpy(char* target, char* source, int n);
int main(void)
{
char source[SIZE], target[SIZE];
while (1)
{
puts("请输入字符串.输入q结束循环");
fgets(source, SIZE, stdin);
if (source[0] == 'q')
goto quit;
mystrncpy(target, source, N);
puts(target);
while (getchar() != '\n')
continue;
}
quit: puts("结束.");
return 0;
}
char* mystrncpy(char* target, char* source, int n)
{
int i;
for (i = 0;i < n;i++)
target[i] = source[i];
target[i] = '\0';
return target;
}
运行结果:
11.8
在官方答案基础上,加了一行输出地址的。
#include <stdio.h>
#define LEN 20
char* string_in(const char* s1, const char* s2);
int main(void)
{
char orig[LEN] = "transportation";
char* find;
puts(orig);
find = string_in(orig, "port");
if (find)
{
puts(find);
printf("地址为%p.\n", find);
}
else
puts("Not found");
find = string_in(orig, "part");
if (find)
puts(find);
else
puts("Not found");
return 0;
}
#include <string.h>
char* string_in(const char* s1, const char* s2)
{
int l2 = strlen(s2);
int tries; /* maximum number of comparisons */
int nomatch = 1; /* set to 0 if match is found */
tries = strlen(s1) + 1 - l2;
if (tries > 0)
while ((nomatch = strncmp(s1, s2, l2)) && tries--)
s1++;
if (nomatch)
return NULL;
else
return (char*)s1; /* cast const away */
}
运行结果:
11.9
跟10.6题基本思想一样。附上10.6的换序代码
void test(int n,double ar[])
{
int i,y;
double x;
y = n / 2;
for (i = 0;i <= y;i++)
{
x = ar[i];
ar[i] = ar[i + n - 1];
ar[i + n - 1] = x;
n -= 2;//保证ar[i+n-1]每次递减1
}
}
11.10
官方答案的删除空格代码
int drop_space(char* s)
{
char* pos;
while (*s) /* or while (*s != '\0') */
{
if (*s == ' ')
{
pos = s;
do
{
*pos = *(pos + 1);
pos++;
} while (*pos);
}
else
s++;
}
}