程序设计题

设计一程序实现功能,处理字符串A,处理规则是:只要B字里面有的字母,不分大小写,一律从A 字符串中删掉。


image

/*************************************************
 *
 *   file name:Pro_StuInfo.c
 *   author   :momolyl@126.com
 *   date     :2024/05/06
 *   function :设计一程序实现功能,处理字符串A,处理规则是:只要B字里面有的字母,不分大小写,一律从A 字符串中删掉。
 *                   (1)请画出此算法的流程图(9分)
 *                   (2)请用C语言编写出对应的代码(20分)
 *   note     :None
 *
 *   CopyRight (c) 2024    momolyl@126.com    All Right Reseverd
 *
 **************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

/*************************************************
 *
 *   函数名称:func
 *   函数功能:将字符串逆序输出
 *   函数参数:
 *            @StrA:字符串A
 *            @StrB:字符串B
 *   返回结果:删除B中字母后的字符串A的地址
 *   注意事项:None
 *   函数作者:momolyl@126.com
 *   创建日期:2024/05/06
 *   函数版本:V1.0
 **************************************************/

char *DelSpecifiled_Str(char *StrA, const char *StrB)
{
    int i, j;
    for (i = 0; StrB[i]; i++) // 遍历字符串B
    {
        // 如果不是字母则跳过本次循环
        if ((StrB[i] < 'A' || StrB[i] > 'Z') && (StrB[i] < 'a' || StrB[i] > 'z'))
        {
            continue;
        }
        for (j = 0; StrA[j]; j++) // 遍历字符串A
        {
            if ((StrA[j] < 'A' || StrA[j] > 'Z') && (StrA[j] < 'a' || StrA[j] > 'z')) // 如果不是字母则跳过本次循环
            {
                continue;
            }
            if (StrA[j] == StrB[i] || StrA[j] == StrB[i] + 32 || StrA[j] == StrB[i] - 32)
            {

                for (int m = j; StrA[m]; m++)
                {
                    StrA[m] = StrA[m + 1];
                }
                j--;
            }
            else
                continue;
        }
    }
    return StrA;
}

int main(void)
{
    char A[] = "helloworlD";
    char B[] = "Ld";
    printf("%s\n", DelSpecifiled_Str(A, B));
    return 0;
}

运行结果
image

注意:
在主函数中,如果使用 char* 定义字符串,调用函数时会出现段错误!

posted @   铃是铃铛的铃  阅读(13)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
点击右上角即可分享
微信分享提示