c语言 9-4

1、

#include <stdio.h>

void null_string(char x[])
{
    x[0] = '\0'; 
}

int main(void)
{
    char str[128];
    printf("str: "); scanf("%s", str);
    
    printf("begin: %s \n", str);
    
    null_string(str);
    
    printf("end: %s \n", str);
    
    return 0;
}

 

 

2、

#include <stdio.h>

void null_test(char x[])
{
    int len = 0;
    while(x[len])
        len++;
    int i;
    if(len > 0)
    {
        for(i = 0; i < len; i++)
        {
            x[i] = '\0';
        }
    }
}

int main(void)
{
    char str[1000];
    printf("str: "); scanf("%s", str);
    
    printf("begin: %s\n", str);
    null_test(str);
    printf("end: %s\n", str);
    
    return 0;
}

 

3、

#include <stdio.h>

void null(char x[])
{
    *x = 0;
}

int main(void)
{
    char str[100];
    printf("str: "); scanf("%s", str);
    
    printf("begin: %s \n", str);
    null(str);
    printf("end: %s \n", str);
    
    return 0;
}

 

posted @ 2021-05-25 22:10  小鲨鱼2018  阅读(48)  评论(0编辑  收藏  举报