C和指针 第6章 指针 6.18 编程练习

1. 编写一个函数,它在一个字符串中进行搜索,查找一个给定字符集合中出现的所有字符。这个函数的原型如下:
    char *find_char( char const *source, char const *chars );
它的基本想法是查找source字符串中匹配chars字符串中任何字符的第1个字符,然后返回一个指向source中第1个匹配所找到的位置的指针。如果source中的所有字符均不匹配chars中的任何字符,就返回一个NULL指针。如果任何一个参数为NULL,或任何一个参数所指向的字符串为空,函数也返回一个NULL指针。
举个例子,假定source指向ABCDEF,如果chars指向XYZ、JURY或QQQQ,函数就返回一个NULL指针;如果chars指向XRCQEF,函数就返回一个指向source中C字符的指针。参数所指向的字符串是绝不会被修改的。
碰巧,C函数库中存在一个名叫strpbrk的函数,它的功能几乎和这个要编写的函数一模一样。但这个程序的目的是让你自己练习操纵指针,所以:
a.不应该使用任何用于操纵字符串的库函数(如strcpy、strcmp、index等);
b.函数中的任何地方都不应该使用下标引用。
解析: 
#include <stdio.h>
#include <stdlib.h>

 char *find_char( char const *source, char const *chars );

int main( void ){
    const char *pc = "ABCDEF";
    char *pfc;
    const char *pchars = "XRZQEF";
    
    pfc = find_char( pc, pchars );
    if( pfc == NULL ){
        printf( "%s's any character is not in %s.\n", pchars, pc );
    } else{
        printf( "%s's first character in %s is %c.\n", pchars, pc, *pfc );
    }

    return EXIT_SUCCESS;
}

char *find_char( char const *source, char const *chars ){
    /* use to specify special condition */ 
    if( source == NULL || chars == NULL || 
        *source == '\0' || *chars == '\0' ){
        return NULL;
    } 
    while( *source != '\0' ){
        char const *pc = chars;
        while( *pc != '\0' ){
            if( *source != *pc ){
                ++pc;
            } else{
                return (char *)source;
            }            
        }
        ++source;
    }
    return NULL;
}
输出:

2.编写一个函数,删除一个字符串的一部分。函数的原型如下:
    int del_substr( char *str, char const *substr )
函数首先应该判断substr是否出现在str中。如果它并未出现,函数就返回0;如果出现,函数应该把str中位于该子串后面的所有字符复制到该子串的位置,从而删除这个子串,然后函数返回1。如果substr多次出现在str中,函数只删除第1次出现的子串。函数的第2个参数绝不会被修改。
举个例子,假定str指向ABCDEFG,如果substr指向FGH、CDF或XABC,函数应该返回0,str未做任何修改;如果substr指向CDE,函数就把str修改为指向ABFG,方法是把F、G和结尾的NUL字节复制到C的位置,然后函数返回1。不论出现什么情况,函数的第2个参数都不应该被修改。
和上题的程序一样:
a.不应该使用任何用于操纵字符串的库函数(如strcpy、strcmp、index等);
b.函数中的任何地方都不应该使用下标引用。  
一个值得注意的地方是,空字符串是每个字符串的一个子串,在空字符串中删除一个空字符串不会产生变化。
解析:
#include <stdio.h>
#include <stdlib.h>

#define TRUE 1
#define FALSE 0

int del_substr( char *str, char const *substr ); 
/* 
** this function isn't the answer the question, 
** it will delete characters in str that appear in substr.
*/ 
int del_substr_2( char *str, char const *substr ); 

int main( void ){
    char str[] = "ABCDEFG";
    char const *substr = "EFH";
    int result;
    
    printf( "original str is %s\n", str );
    result = del_substr( str, substr );
    if( result == 0 ){
        printf( "the characters in %s aren't included in %s perfectly.\n", substr, str );
    } else{
        printf( "delete substr %s successed, present str is %s.\n", substr, str );
    } 

    return EXIT_SUCCESS;
}

int del_substr( char *str, char const *substr ){
    char *str_temp;
    /* use to indicate whether find the same substr */
    int flag = 0; 
    /* substr is a null string */
    if( *substr == '\0' ){
        return TRUE;
    }
    for( ; *str != '\0'; ++str ){
        str_temp = str;
        char const *sub_temp = substr;
        while( *str_temp == *sub_temp && *sub_temp != '\0'){
            str_temp += 1;
            sub_temp += 1;
        }
        if( *sub_temp == '\0' ){
            flag = 1;
            break;
        }
    }
    /* reach the end of substr */
    if( flag ){
        /* don't reach the end of str */
        for( ; *str_temp != '\0'; ++str_temp ){ 
            *str++ = *str_temp;
        }
        *str = '\0'; 
        return TRUE;
    } 
    return FALSE;
}

int del_substr_2( char *str, char const *substr ){
    char *str_temp;
    char *str_temp2;
    int flag = FALSE;
    
    if( *substr == '\0' ){
        return TRUE;
    }
    
    while( *str != '\0' ){
        char const *pc = substr;
        while( *pc != '\0' ){

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weixin_40186813

你的能量无可限量。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值