《C和指针》第六章书后编程

《C和指针》第六章书后编程_第1张图片

#include 
#include 

char *find_char(char const *source,char const *chars)
{
    if(source == NULL || chars == NULL)
        return NULL;
    else
    {
        char *temp=chars;
        while(*source != '\0')
        {
            while(*chars != '\0')
            {
                if(*source == *chars)
                    return source;
                else
                    chars++;
            }
            source++;
            chars=temp;
        }
        return NULL;
    }
}

int main()
{
    char *source="ABCDEF",*chars="cxAF";
    char *res=find_char(source,chars);
    if(res != NULL)
        printf("%c",*res);
    else
        printf("NULL");

    return 0;
}

《C和指针》第六章书后编程_第2张图片

#include 
#include 

int del_substr(char *str, char const *substr)
{
    char *substr_head=substr,*temp;
    while(*str != '\0')
    {
        if(*str == *substr)
        {
            temp=str;
            while(*substr != '\0' && *substr == *str)
            {
                substr++;
                str++;
            }
            if(*substr == '\0')
            {
                while(*str != '\0')
                {
                    *temp++ = *str++;
                }
                *temp='\0';
                return 1;
            }
            else
                substr=substr_head;
        }
        str++;
    }
    return 0;
}
int main()
{
    char str[]="ABCDEFG",*substr="CD";
    int res=del_substr(str,substr);
    printf("%d\n%s\n",res,str);
    return 0;
}

《C和指针》第六章书后编程_第3张图片

#include 
#include 

void reverse_string(char *string)
{
    char *head=string,*tail=string,temp;
    while(*tail != '\0')
        tail++;
    tail--;
    while(head<tail)
    {
        temp=*head;
        *head=*tail;
        *tail=temp;
        head++;
        tail--;
    }
}

int main()
{
    char str[]="ABCDEfFGjhj";
    printf("%s\n",str);
    reverse_string(str);
    printf("%s\n",str);
    return 0;
}

《C和指针》第六章书后编程_第4张图片
没有使用指针来访问数组

#include 
#include 
#include 

#define SIZE 1000
#define true 1
#define false 0
int main()
{
    char array[SIZE];
    memset(array,true,SIZE);
    array[0]=array[1]=false;
    /*for(int i=0;i<10;i++)
        printf("%d\n",array[i]);*/
    for(int i=2;i<SIZE;i++)
    {
        if(array[i]==true)
        {
            for(int j=i*i;j<SIZE;j+=i)
                array[j]=false;
        }
    }
    for(int i=0;i<SIZE;i++)
        if(array[i]==true)
            printf("%d ",i);

    return 0;
}

在这里插入图片描述

#include 
#include 
#include 
#include 

#define SIZE 1000
#define true 1
#define false 0

void set_bit(char bit_array[],unsigned bit_number);
void clear_bit(char bit_array[],unsigned bit_number);
void assign_bit(char bit_array[],unsigned bit_number,int value);
int test_bit(char bit_array[],unsigned bit_number);

int character_offset(unsigned bit_number);
int bit_offset(unsigned bit_number);

int main()
{
    char array[SIZE];
    memset(array,0xff,SIZE);
    //array[0]=array[1]=false;
    clear_bit(array,0);
    clear_bit(array,1);
    /*for(int i=0;i<10;i++)
        printf("%d\n",array[i]);*/
    for(int i=2;i<SIZE*CHAR_BIT;i++)
    {
        if(test_bit(array,i))
        {
            for(int j=i*i;j<SIZE*CHAR_BIT;j+=i)
                clear_bit(array,j);
        }
    }
    for(int i=0;i<SIZE*CHAR_BIT;i++)
        if(test_bit(array,i))
            printf("%d ",i);

    return 0;
}

int character_offset(unsigned bit_number)
{
    return bit_number / CHAR_BIT;
}

int bit_offset(unsigned bit_number)
{
    return bit_number % CHAR_BIT;
}

void set_bit(char bit_array[],unsigned bit_number)
{
    bit_array[character_offset(bit_number)] |= 1<<bit_offset(bit_number);
}

void clear_bit(char bit_array[],unsigned bit_number)
{
    bit_array[character_offset(bit_number)] &= ~(1<<bit_offset(bit_number));
}

void assign_bit(char bit_array[],unsigned bit_number,int value)
{
    if(value)
        set_bit(bit_array,bit_number);
    else
        clear_bit(bit_array,bit_number);
}

int test_bit(char bit_array[],unsigned bit_number)
{
    char ch = bit_array[character_offset(bit_number)]>>bit_offset(bit_number);
    if(ch & 1)
        return 1;
    else
        return 0;
}

《C和指针》第六章书后编程_第5张图片

#include 
#include 
#include 

#define SIZE 10000
#define true 1
#define false 0
#define interval 1000
int main()
{
    char array[SIZE];
    memset(array,true,SIZE);
    array[0]=array[1]=false;
    /*for(int i=0;i<10;i++)
        printf("%d\n",array[i]);*/
    for(int i=2;i<SIZE;i++)
    {
        if(array[i]==true)
        {
            for(int j=i*i;j<SIZE;j+=i)
                array[j]=false;
        }
    }

    /*for(int i=0;i
    for(int i=0;i<SIZE;i+=interval)
    {
        int count=0;
        for(int j=i;j<(i+interval);j++)
            if(array[j]==true)
                count++;
        printf("%d-%d:%d\n",i,i+interval,count);
    }

    return 0;
}

你可能感兴趣的:(C)