#include <stdio.h>
#include <stdlib.h>
char *find_char( char const *source, char const *chars )
{
int iSource = 0;
while (*(source + iSource) != '\000')
{
int iChars = 0;
while (*(chars + iChars) != '\000')
{
if (*(chars + iChars) == *(source + iSource))
return (source + iSource);
++iChars;
}
++iSource;
}
return NULL;
}
int main()
{
char *pchSource = "ABCDEF";
char *pNull = "XYZ";
char *pChars = "XRCQEF";
if (find_char(pchSource, pNull) != NULL)
printf("not have %c\n", *find_char(pchSource, pNull));
printf("have %c\n", *find_char(pchSource, pChars));
system("pause");
return EXIT_SUCCESS;
}
#include <stdio.h>
#include <stdlib.h>
int del_substr( char *str, char const *substr )
{
printf("str1 = %s, &str1 = %d\n", str, str);
if (substr == NULL || *substr == '\000')
return 0;
int iStrSize = 0; // 记录字符串的字符数
while (*(str + iStrSize) != '\000')
{
++iStrSize;
}
char *pCh = (char *)malloc(sizeof(char) * iStrSize); // 创建一个中间字符串用于保存此时的str
*(pCh + iStrSize) = '\000'; // 最后一定要赋空值
int iStr = 0; // 记录字串所在主串的第一次出现的位置
int iSonCount = 0;
for (int i = 0; i < iStrSize; ++i)
{
iSonCount = 0;
while (*(substr + iSonCount) != '\000' && *(str + iStr + iSonCount) != '\000' && *(str + iStr + iSonCount) == *(substr + iSonCount))
{
++iSonCount;
}
if (*(substr + iSonCount) != '\000')
++iStr;
*(pCh + i) = *(str + i);
}
int iRet = 0;
if (*(substr + iSonCount) == '\000')
{
iRet = 1;
// 拷贝字符串,先将str的字符串赋值到pCh
int m = 0;
for (int n = 0; n < iStrSize; ++n)
{
if (n < iStr || n >= iStr + iSonCount)
{
*(str + m) = *(pCh + n);
++m;
}
}
*(str + m) = '\000';
printf("str2 = %s, &str2 = %d\n", str, str);
}
return iRet;
}
#define PARENT_COUNT 10
#define SON_COUNT 3
int main()
{
// char *pchSour = "ABCDEF"; // 直接定义的是字符串常量,只读
char *pchParent = (char *)malloc(sizeof(char) * PARENT_COUNT);
char ch = 'A';
for (int i = 0; i < PARENT_COUNT; ++i)
{
*(pchParent + i) = ch++;
}
*(pchParent + PARENT_COUNT) = '\000';
char *pchSon = (char *)malloc(sizeof(char) * SON_COUNT);
*(pchSon + 0) = 'C';
*(pchSon + 1) = 'D';
*(pchSon + 2) = '\000';
int iRet = del_substr( pchParent, pchSon );
printf("str2 = %s, &str2 = %d\n", pchParent, pchParent);
system("pause");
return EXIT_SUCCESS;
}
#include <stdio.h>
#include <stdlib.h>
void reverse_string( char *string )
{
if (*string == '\000')
return;
int iStrCount = 0;
while (*(string + iStrCount) != '\000')
{
++iStrCount;
}
// char *pchStr = (char *)malloc(sizeof(char) * iStrCount);
for(int i = 0; i < iStrCount / 2; ++i)
{
char ch = *(string + iStrCount - i - 1);
*(string + iStrCount - i - 1) = *(string + i);
*(string + i) = ch;
}
*(string + iStrCount) = '\000';
}
#define STR_COUNT 10
int main()
{
char *pchStr = (char *)malloc(sizeof(char) * STR_COUNT);
char ch = 'A';
for (int i = 0; i < STR_COUNT; ++i)
*(pchStr + i) = ch++;
*(pchStr + STR_COUNT) = '\000';
printf("input string is : %s\n", pchStr);
reverse_string(pchStr);
printf("output string is : %s\n", pchStr);
system("pause");
return EXIT_SUCCESS;
}
#include <stdio.h>
#include <stdlib.h>
#define ARRAY_COUNT 1000
void GetPrime(char *pchPrime, int iCount, int iFirst)
{
// 质数从2开始,将2后面的2的倍数都赋0
for (int i = iFirst; i < iCount; i = iFirst + i)
{
if (*(pchPrime + i) == 0 || i % iFirst != 0 || i == iFirst)
continue;
else if (i % iFirst == 0)
*(pchPrime + i) = 0;
}
if (iFirst + 1 < iCount)
GetPrime(pchPrime, iCount, iFirst + 1);
}
int main()
{
// 创建数组
char chArray[ARRAY_COUNT] = {0};
for (int i = 1; i < ARRAY_COUNT; ++i)
chArray[i] = 1;
GetPrime(chArray, ARRAY_COUNT, 2);
for (int i = 2; i < ARRAY_COUNT; ++i)
{
if (chArray[i] == 1)
printf("%d, ", i);
}
printf("\n");
system("pause");
return EXIT_SUCCESS;
}
/* 这题没什么好看的
修改前一题的Eratosthenes程序(即第4题的代码),使用位的数组而不是字符数组,这里要用到第5章编程练习中所开发的位数组函数。
这个修改使程序的空间效率进一步提高,不过代价是时间效率降低。在你的系统中,使用这个方法,你所能找到的最大质数是多少?
#include <stdio.h>
#include <stdlib.h>
void set_bit(char bit_array[], unsigned bit_number); // 把指定的位设置为1
void clear_bit(char bit_array[], unsigned bit_number); // 把指定的位清零
void assign_bit(char bit_array[], unsigned bit_number); // 把指定的位清0,否则设置为1。
int test_bit(char bit_array[], unsigned bit_number); // 如果参数中指定的位不是0,函数就返回真,否则返回假
int main()
{
system("pause");
return EXIT_SUCCESS;
}
void set_bit(char bit_array[], unsigned bit_number)
{
int iBitCount = 0;
while (bit_array[iBitCount] != '\000')
{
++iBitCount;
}
if (bit_number <= iBitCount)
bit_array[bit_number] = 1;
else
printf("bit_number out of array range");
}
void clear_bit(char bit_array[], unsigned bit_number)
{
int iBitCount = 0;
while (bit_array[iBitCount] != '\000')
{
++iBitCount;
}
if (bit_number <= iBitCount)
bit_array[bit_number] = 0;
else
printf("bit_number out of array range");
}
void assign_bit(char bit_array[], unsigned bit_number)
{
int iBitCount = 0;
while (bit_array[iBitCount] != '\000')
{
++iBitCount;
}
if (bit_number <= iBitCount)
bit_array[bit_number] = (bit_array[bit_number] == 0) ? 1 : 0;
else
printf("bit_number out of array range");
}
int test_bit(char bit_array[], unsigned bit_number)
{
int iRet = 0;
int iBitCount = 0;
while (bit_array[iBitCount] != '\000')
{
++iBitCount;
}
if (bit_number <= iBitCount)
iRet = (bit_array[bit_number] == 0) ? 0 : 1;
else
printf("bit_number out of array range");
return iRet;
}
#include <stdio.h>
#include <stdlib.h>
#define ARRAY_COUNT 1000
void GetPrime(char *pchPrime, int iCount, int iFirst)
{
// 质数从2开始,将2后面的2的倍数都赋0
for (int i = iFirst; i < iCount; i = iFirst + i)
{
if (*(pchPrime + i) == 0 || i % iFirst != 0 || i == iFirst)
continue;
else if (i % iFirst == 0)
*(pchPrime + i) = 0;
}
if (iFirst + 1 < iCount)
GetPrime(pchPrime, iCount, iFirst + 1);
}
int main()
{
// 创建数组
char chArray[ARRAY_COUNT] = {0};
for (int i = 1; i < ARRAY_COUNT; ++i)
chArray[i] = 1;
GetPrime(chArray, ARRAY_COUNT, 2);
for (int i = 2; i < ARRAY_COUNT; ++i)
{
if (chArray[i] == 1)
printf("%d, ", i);
}
printf("\n");
system("pause");
return EXIT_SUCCESS;
}
*/
#include <stdio.h>
#include <stdlib.h>
#define ARRAY_COUNT 1000
int GetPrimeCounts(int iNumTotal, int iStart) // 质数必须大于2,起点和终点共1000个数,即0-999,1000-1999
{
int iRet = 0;
for (int i = iStart; i < iNumTotal + iStart; ++i)
{
if (i < 2)
continue;
else if (i < 4)
{
++iRet;
// printf("%d, ", i);
}
else
{
int k = 2;
while (i % k != 0)
{
++k;
}
if (k == i)
{
++iRet;
// printf("%d, ", i);
}
}
}
return iRet;
}
int main()
{
int iCounts = 0;
for (int i = 0; i < 10; ++i)
{
iCounts = GetPrimeCounts(ARRAY_COUNT, i * ARRAY_COUNT);
printf("\nCount is : %d\n", iCounts);
}
system("pause");
return EXIT_SUCCESS;
}