1、请编写一个函数,它在一个字符串中进行搜索,查找所有在一个给定字符集合中出现的字符。这个函数的原型应该如下:
char *find_char(char const *source,char const *chars);
它的基本想法是查找source字符串中匹配chars字符串中任何字符的第一个字符,函数然后返回一个指向source中第一个匹配所找到的位置的指针,如果任何一个参数NULL,或任何一个参数所指向的字符串为空,函数也返回一个NULL指针。
举个例子,假定source指向ABCDEF。如果chars指向XYZ、JURY或QQQQ,函数就返回一个NULL指针。如果chars指向XRCQEF,函数就返回一个指向source中C字符的指针。参数所指向的字符串是不会被修改的。
#include <stdio.h>
char *find_char(char const *source, char const *chars);
int main(int argc, char *argv[])
{
char *pc;
pc = find_char(argv[1], argv[2]);
if (pc == NULL)
{
printf("pc is NULL\n");
}
else
{
printf("pc is %c\n", *pc);
}
return 0;
}
char *find_char(char const *source, char const *chars)
{
char *p_source = source;
char *p_chars = chars;
if ((p_source == NULL) || (p_chars == NULL))
{
return NULL;
}
while (*p_source != '\0')
{
p_chars = chars;
while (*p_chars != '\0')
{
//printf("compare %c\n", *p_chars);
if (*p_source == *p_chars)
{
return p_source;
}
p_chars++;
}
//printf("next one\n");
p_source++;
}
return NULL;
}
2、请编写一个函数,删除一个字符串的一部分。函数的原型如下:
int del_substr(char *str,char const *substr);
函数首先应该判断substr是否出现在sub中。如果它并未出现,函数就返回0,如果出现,函数应该把str中位于该子串后面的所有字符复制到该子串的位置,从而删除这个子串,然后函数返回1, 如果substr在函数中出现多次,函数只删除第一次出现的子串。函数的第二个参数不能被修改。
举个例子,假定str指向ABCDEFG。如果substr指向FGH,CDF或XABC,函数返回0,str未作任何修改,但如果substr指向CDE,函数就把str修改为指向ABFG,方法是把FG和结尾的NUL字节复制到C的位置,然后函数返回1。不论出现什么情况,第二个参数都不能被修改。
#include <stdio.h>
int del_substr(char *str, char const *substr);
int main(int argc, char *argv[])
{
int result;
char string[] = "ABCDEFG";
result = del_substr(string, argv[1]);
if (result)
{
printf("delect succeed\n");
}
else
{
printf("delect fault\n");
}
puts(string);
return 0;
}
int del_substr(char *str, char const *substr)
{
char *p_substr;
char *p_str;
if ((str != NULL) && (substr == NULL))
{
return 1;
}
for (;*str != '\0';str++)
{
p_str = str;
p_substr = substr;
while (*p_substr != '\0')
{
if (*p_str != *p_substr)
{
break;
}
p_str++;
p_substr++;
}
if (*p_substr == '\0')
{
while (*str++ = *p_str++)
;
return 1;
}
}
return 0;
}
3、编写函数reverse_string,它的原型如下:
void reverse_string(char *string);
函数把参数字符串中的字符反向排列,请使用指针而不是数组下标,不要使用任何C函数库中用于操纵字符串的函数。提示:不需要声明一个局部变量数组来临时存储参数字符串。
#include <stdio.h>
void reverse_string(char *string)
{
int i;
int len;
char tmp;
char *str_end;
str_end = string;
for (len = 0; *str_end != '\0'; str_end++)
{
len++;
}
str_end--;
//printf("len: %d\n", len);
for (i = 0; i < len/2; i++)
{
tmp = *string;
*string = *str_end;
*str_end = tmp;
string++;
str_end--;
}
}
int main(int argc, char *argv[])
{
reverse_string(argv[1]);
puts(argv[1]);
return 0;
}
4、质数就是只能被1和本身整除的整数,Eratosthenes筛选法是一种计算质数的有效办法。这个算法的第一步就是写下所有从2至某个上限之间的所有整数,在算法剩余部分,你遍历整个列表并剔除所有不是质数的整数。后面的步骤是这样的,找到列表中的第一个不被剔除的数也就是2,然后将列表中所有2的倍数全部剔除,因为它们都可以被2整除,因此不是质数。接着,再回到列表的头部重新开始,此时列表中尚未被剔除的第一个数是3,所以在3之后把每逢第3个数的倍数剔除,完成这一步骤之后,再回到列表开头,3后面的下一个数是4,但它是2的倍数,已经被剔除,所以将其跳过,轮到5,将所有5的倍数全部剔除,这样以此类推,反复执行,最后列表中未被剔除的数均为质数。
编写一个程序,实现这个算法,使用数组表示你的列表。每个数组元素的值用于标记对应的数是否剔除。开始时所有元素的值都设置为FALSE。如果你的程序运行于16位机器上,小心考虑是不是需要把某个变量声明为long。一开始先使用包含1000个元素的数组。如果你使用字符数组,使用相同的空间,你将会比使用整数数组找到更多的质数,你可以使用下标来表示指向数组首元素和尾元素的指针,但你应该使用指针来访问数组元素。
注意除了2以外,所有的偶数都不是质数,稍微多想一下,你可以使程序的空间效率大为提高,方法是数组中的所有元素只对应奇数,这样,你在相同的数组空间内,你可以寻找到的质数的个数大约是原先的两倍。
#include <stdio.h>
//#define MAX 1000
#define TRUE 1
#define FALSE 0
void eratosthenes(char list[], int max);
int main(int argc, char *argv[])
{
int i, count;
int max;
if (atoi(argv[1]) % 2)
max = atoi(argv[1]) / 2;
else
max = atoi(argv[1]) / 2 - 1;
char list[max];
for (i = 0; i < max; i++)
{
list[i] = TRUE;
}
eratosthenes(list, max);
count = 0;
printf("2 ");
for (i = 0; i < max; i++)
{
if (list[i] == TRUE)
{
printf("%d ", (i*2 + 3));
count++;
if (count % 10 == 0){
printf("\n");
}
}
}
printf("\n");
return 0;
}
void eratosthenes(char list[], int max)
{
int i, num;
char *p;
char *p_del;
num = 3;
p = list;
for (i = 0; i < (max - 1); i++)
{
if (*p == TRUE)
{
for (p_del = (p + num); p_del < &list[max]; p_del += num)
{
*p_del = FALSE;
}
}
num += 2;
p++;
}
}
5、修改前一题的Eratosthenes程序,使用位的数组而不是字符数组,这里要用到第5章编程练习中所开发的位数组函数。这个修改使程序的空间效率进一步提高,不过代价是时间效率降低,在你的系统中,使用这个方法,你所能找到的最大质数是多少?
#include <stdio.h>
void eratosthenes(char list[], int max);
void set_bit(char bit_array [ ], unsigned int bit_number);
void clear_bit(char bit_array [ ], unsigned int bit_number);
int test_bit(char bit_array [ ], unsigned int bit_number);
int main(int argc, char *argv[])
{
int i, count = 0;
unsigned int max;
char list_bit[max / 8];
if (atoi(argv[1]) % 2)
max = atoi(argv[1]) / 2;
else
max = atoi(argv[1]) / 2 - 1;
for (i = 0; i < max; i++)
{
set_bit(list_bit, i);
}
eratosthenes(list_bit, max);
printf("2 ");
for (i = 0; i < max; i++)
{
if (test_bit(list_bit, i)){
printf("%d ", (i*2 + 3));
count++;
if (count % 10 == 0)
printf("\n");
}
}
printf("\n");
return 0;
}
void eratosthenes(char list[], int max)
{
int i, j;
for (i = 0; i < (max - 1); i++)
{
if (test_bit(list, i))
{
for (j = i + (i*2 + 3); j <= max; j += (i*2 + 3))
{
printf("%d ", test_bit(list, j));
clear_bit(list, j);
printf("%d\n", test_bit(list, j));
}
}
}
}
6、大质数是不是和小质数一样多,换句话说,在50000和51000之间的质数是不是和1000000到1001000之间的质数一样多?使用前面的程序计算0到1000之间有多少个质数,1000到2000之间有多少个质数?以此每隔1000类推,到1000000有多少个质数,每隔1000个数中质数的质量的数量呈什么趋势?
#include <stdio.h>
//#define MAX 1000
#define TRUE 1
#define FALSE 0
void eratosthenes(char list[], int max);
int main(int argc, char *argv[])
{
int i, j, count;
int max;
if (atoi(argv[1]) % 2)
max = atoi(argv[1]) / 2;
else
max = atoi(argv[1]) / 2 - 1;
char list[max];
for (i = 0; i < max; i++)
{
list[i] = TRUE;
}
eratosthenes(list, max);
count = 1;
for (j = 0; j < 500; j++)
if (list[j] == TRUE)
count++;
printf("the number of primes between 0 and 1000: %d\n", count);
for (i = 500; i+500 <= max + 1; i += 500){
count = 0;
for (j = 0; j < 500; j++)
if (list[j + i] == TRUE)
count++;
printf("the number of primes between %d and %d: %d\n", i*2, i*2 + 1000, count);
}
return 0;
}
void eratosthenes(char list[], int max)
{
int i, num;
char *p;
char *p_del;
num = 3;
p = list;
for (i = 0; i < (max - 1); i++)
{
if (*p == TRUE)
{
for (p_del = (p + num); p_del < &list[max]; p_del += num)
{
*p_del = FALSE;
}
}
num += 2;
p++;
}
}